本文整理汇总了C#中System.Reflection.Emit.ModuleBuilder类的典型用法代码示例。如果您正苦于以下问题:C# ModuleBuilder类的具体用法?C# ModuleBuilder怎么用?C# ModuleBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ModuleBuilder类属于System.Reflection.Emit命名空间,在下文中一共展示了ModuleBuilder类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CodeGenerator
//引入命名空间
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;
public class CodeGenerator
{
AssemblyBuilder myAssemblyBuilder;
public CodeGenerator()
{
// Get the current application domain for the current thread.
AppDomain myCurrentDomain = AppDomain.CurrentDomain;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";
// Define a dynamic assembly in the current application domain.
myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
(myAssemblyName, AssemblyBuilderAccess.Run);
// Define a dynamic module in this assembly.
ModuleBuilder myModuleBuilder = myAssemblyBuilder.
DefineDynamicModule("TempModule");
// Define a runtime class with specified name and attributes.
TypeBuilder myTypeBuilder = myModuleBuilder.DefineType
("TempClass",TypeAttributes.Public);
// Add 'Greeting' field to the class, with the specified attribute and type.
FieldBuilder greetingField = myTypeBuilder.DefineField("Greeting",
typeof(String), FieldAttributes.Public);
Type[] myMethodArgs = { typeof(String) };
// Add 'MyMethod' method to the class, with the specified attribute and signature.
MethodBuilder myMethod = myTypeBuilder.DefineMethod("MyMethod",
MethodAttributes.Public, CallingConventions.Standard, null,myMethodArgs);
ILGenerator methodIL = myMethod.GetILGenerator();
methodIL.EmitWriteLine("In the method...");
methodIL.Emit(OpCodes.Ldarg_0);
methodIL.Emit(OpCodes.Ldarg_1);
methodIL.Emit(OpCodes.Stfld, greetingField);
methodIL.Emit(OpCodes.Ret);
myTypeBuilder.CreateType();
}
public AssemblyBuilder MyAssembly
{
get
{
return this.myAssemblyBuilder;
}
}
}
public class TestClass
{
[PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
public static void Main()
{
CodeGenerator myCodeGenerator = new CodeGenerator();
// Get the assembly builder for 'myCodeGenerator' object.
AssemblyBuilder myAssemblyBuilder = myCodeGenerator.MyAssembly;
// Get the module builder for the above assembly builder object .
ModuleBuilder myModuleBuilder = myAssemblyBuilder.
GetDynamicModule("TempModule");
Console.WriteLine("The fully qualified name and path to this "
+ "module is :" +myModuleBuilder.FullyQualifiedName);
Type myType = myModuleBuilder.GetType("TempClass");
MethodInfo myMethodInfo =
myType.GetMethod("MyMethod");
// Get the token used to identify the method within this module.
MethodToken myMethodToken =
myModuleBuilder.GetMethodToken(myMethodInfo);
Console.WriteLine("Token used to identify the method of 'myType'"
+ " within the module is {0:x}",myMethodToken.Token);
object[] args={"Hello."};
object myObject = Activator.CreateInstance(myType,null,null);
myMethodInfo.Invoke(myObject,args);
}
}