The namespace of System.Reflection.Emit is the rennovated namespace for reflection based APIs. In this topic, we are going to discuss the how to use the ModuleBuilder from this particular namespace.
the original MSDN documentation on the ModuleBuilder is avaiable here in this page: Module Builder Class
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"; // looks like we are creating the assemblies on the fly // Define a dynamic assembly in the current application domain. myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run); // indicate what kind of acces the generated assemblies is supposed to used! // now we have the Assembly, we can then create the 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 a Field, which is called "Greeting" to the class, with the specified attribute and type. FieldBuilder greetingField = myTypeBuilder.DefineField("Greeting", typeof(string), FieldAttributes.Public); Type[] myMethodArgs = { typeof(string) }; // type of the Method Args is defined as such // Add 'MyMethod' method to the class, with teh specified attributes and signature MethodBuilder myMethod = myTypeBuilder.DefineMethod("MyMethod", MethodAttributes.Public, CallingConventions.Standard, null, myMethodArgs); ILGenerator methodIL = myMethod.GetILGenerator(); methodIL.EmitWriteLine("In the method..."); // the following code has the same effect as // anonymous_fun(string arg) { // GreetingField = arg; // return; // } methodIL.Emit(OpCodes.Ldarg_0); // load this pointer methodIL.Emit(OpCodes.Ldarg_1); // load the first argument, which "value" methodIL.Emit(OpCodes.Stfld, greetingField); // store the value of "value" to the GreetingField methodIL.Emit(OpCodes.Ret); // return from the call myTypeBuilder.CreateType(); // Generate the code } 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 myModuleBuidler = myAssemblyBuilder.GetDynamicModule("TempModule"); // Display the fully qualified name of the moduel Console.WriteLine("The fully qualified name and path to this " + "module is :" + myModuleBuidler.FullyQualifiedName); // Type myType = myModuleBuidler.GetType("TempClass"); MethodInfo myMethodInfo = myType.GetMethod("MyMethod"); // Get the token used to identify the method iwthin this module // SO be careful that the method does not return the MethodInfo, but rather return // the method tokens MethodToken myMethodToken = myModuleBuidler.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); } }
As you can see:
- there are different types of builder, e.g. ModuleBuilder, TypeBuilder, FieldBuidler, MethodBuilder which build Module, Type, Field or Method;
- The various xxxBuilder class has the DefineXXX method to get a builder of subordinate constructs, e.g. ModuleBuilder.DefienType return TypeBuilder.
- Each Builder is a subclass to XXXInfo, e.g. FiledBuilder is subclass to FieldInfo.
- To generate the IL code, you may use the ILGenerator, and there is a bunch of Emit calls to generate the body of the method.
- From each builder, you can query some adjunct construct, e.g. you can get ModuleBuilder from AssemblyBuilder...