本文整理匯總了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);
}
}