Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ModuleBuilder Class

Defines and represents a module in a dynamic assembly.

System.Object
  System.Reflection.Module
    System.Reflection.Emit.ModuleBuilder

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public class ModuleBuilder : Module

The ModuleBuilder type exposes the following members.

  NameDescription
Public propertyAssemblyGets the dynamic assembly that defined this instance of ModuleBuilder. (Overrides Module.Assembly.)
Public propertyMetadataTokenGets a token that identifies the current dynamic module in metadata. (Overrides Module.MetadataToken.)
Public propertyNameA string that indicates that this is an in-memory module. (Overrides Module.Name.)
Top

  NameDescription
Public methodCreateGlobalFunctionsCompletes the global function definitions and global data definitions for this dynamic module.
Public methodDefineDocumentDefines a document for source code.
Public methodDefineEnumDefines an enumeration type in this module.
Public methodDefineGlobalMethod(String, MethodAttributes, Type, Type[])Defines a global method with the specified name, attributes, return type, and parameter types.
Public methodDefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[])Defines a global method with the specified name, attributes, calling convention, return type, and parameter types.
Public methodDefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])Defines a global method with the specified name, attributes, calling convention, return type, custom modifiers for the return type, parameter types, and custom modifiers for the parameter types.
Public methodDefineType(String)Defines a private type with the specified name in this module.
Public methodDefineType(String, TypeAttributes)Defines a type with the specified type name and attributes in this module.
Public methodDefineType(String, TypeAttributes, Type)Defines a type with the specified type name, attributes, and base type in this module.
Public methodDefineType(String, TypeAttributes, Type, Type[])Defines a type with the specified type name, attributes, base type, and implemented interfaces in this module.
Public methodEqualsReturns a value that indicates whether this instance is equal to the specified object. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetArrayMethodReturns the named method on an array class.
Public methodGetArrayMethodTokenReturns the token for the named method on an array class.
Public methodGetConstructorTokenReturns the token used to identify the specified constructor within this module.
Public methodGetCustomAttributes(Boolean)Returns all the custom attributes that have been applied to the current ModuleBuilder. (Overrides Module.GetCustomAttributes(Boolean).)
Public methodGetCustomAttributes(Type, Boolean)Returns all the custom attributes that have been applied to the current ModuleBuilder, and that derive from a specified attribute type. (Overrides Module.GetCustomAttributes(Type, Boolean).)
Public methodGetField(String)Returns the global field that has the specified name. (Inherited from Module.)
Public methodGetField(String, BindingFlags)Returns a module-level field, defined in the .sdata region of the portable executable (PE) file, that has the specified name and binding attributes. (Overrides Module.GetField(String, BindingFlags).)
Public methodGetFields()Returns the global fields defined on the module. (Inherited from Module.)
Public methodGetFields(BindingFlags)Returns all fields defined in the .sdata region of the portable executable (PE) file that match the specified binding flags. (Overrides Module.GetFields(BindingFlags).)
Public methodGetFieldTokenReturns the token used to identify the specified field within this module.
Public methodGetHashCodeReturns the hash code for this instance. (Overrides Object.GetHashCode().)
Public methodGetMethod(String)Returns the global method that has the specified name. (Inherited from Module.)
Public methodGetMethod(String, Type[])Returns the global method that matches the specified name and parameter types. (Inherited from Module.)
Public methodGetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])Returns the global method that has the specified name, binding information, calling convention, and parameter types and modifiers. (Inherited from Module.)
Protected methodGetMethodImplReturns the module-level method that matches the specified criteria. (Overrides Module.GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]).)
Public methodGetMethods()Returns the global methods defined on the module. (Inherited from Module.)
Public methodGetMethods(BindingFlags)Returns all the methods that have been defined at the module level for the current ModuleBuilder, and that match the specified binding flags. (Overrides Module.GetMethods(BindingFlags).)
Public methodGetMethodTokenReturns the token used to identify the specified method within this module.
Public methodGetSignatureToken(SignatureHelper)Defines a token for the signature that is defined by the specified SignatureHelper.
Public methodGetSignatureToken(Byte[], Int32)Defines a token for the signature that has the specified character array and signature length.
Public methodGetStringConstantReturns the token of the given string in the module’s constant pool.
Public methodGetType()Gets the Type of the current instance. (Inherited from Object.)
Public methodGetType(String)Gets the named type defined in this module. (Overrides Module.GetType(String).)
Public methodGetType(String, Boolean)Gets the named type defined in this module, optionally ignoring the case of the type name. (Overrides Module.GetType(String, Boolean).)
Public methodGetType(String, Boolean, Boolean)Gets the named type defined in this module, optionally ignoring the case of the type name. Optionally throws an exception if the type is not found. (Overrides Module.GetType(String, Boolean, Boolean).)
Public methodGetTypesReturns all the classes defined within this module. (Overrides Module.GetTypes().)
Public methodGetTypeToken(String)Returns the token used to identify the type with the specified name.
Public methodGetTypeToken(Type)Returns the token used to identify the specified type within this module.
Public methodIsDefinedReturns a value that indicates whether the specified attribute type has been applied to this module. (Overrides Module.IsDefined(Type, Boolean).)
Public methodIsTransientReturns a value that indicates whether this dynamic module is transient.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodResolveField(Int32)Returns the field identified by the specified metadata token. (Inherited from Module.)
Public methodResolveField(Int32, Type[], Type[])Returns the field identified by the specified metadata token, in the context defined by the specified generic type parameters. (Overrides Module.ResolveField(Int32, Type[], Type[]).)
Public methodResolveMethod(Int32)Returns the method or constructor that is identified by the specified metadata token. (Inherited from Module.)
Public methodResolveMethod(Int32, Type[], Type[])Returns the method or constructor identified by the specified metadata token, in the context defined by the specified generic type parameters. (Overrides Module.ResolveMethod(Int32, Type[], Type[]).)
Public methodResolveStringReturns the string identified by the specified metadata token. (Overrides Module.ResolveString(Int32).)
Public methodResolveType(Int32)Returns the type that is identified by the specified metadata token. (Inherited from Module.)
Public methodResolveType(Int32, Type[], Type[])Returns the type identified by the specified metadata token, in the context defined by the specified generic type parameters. (Overrides Module.ResolveType(Int32, Type[], Type[]).)
Public methodSetCustomAttributeApplies a custom attribute to this module by using a custom attribute builder.
Public methodToStringReturns the name of the module. (Inherited from Module.)
Top

In Silverlight, you can create only one dynamic module per dynamic assembly.

To get an instance of ModuleBuilder, use the AssemblyBuilder.DefineDynamicModule method.

The following sample demonstrates the use of ModuleBuilder to create a dynamic module. The ModuleBuilder is created by calling DefineDynamicModule in AssemblyBuilder.


using System;
using System.Reflection;
using System.Reflection.Emit;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Get the current application domain for the current thread.
      AppDomain myCurrentDomain = AppDomain.CurrentDomain;
      AssemblyName myAssemblyName = new AssemblyName("TempAssembly");

      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder 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, typeof(String), myMethodArgs);

      // Method to concatenate two strings.
      MethodInfo stringConcat = typeof(String).GetMethod("Concat", 
         BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder, 
         new Type[] { typeof(String), typeof(String) }, 
         null);

      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.Ldstr, "Hello, ");
      methodIL.Emit(OpCodes.Ldarg_1);
      methodIL.Emit(OpCodes.Call, stringConcat);
      methodIL.Emit(OpCodes.Ret);

      Type myType = myTypeBuilder.CreateType();
      MethodInfo myMethodInfo =
                              myType.GetMethod("MyMethod");

      // Get the token used to identify the method within this module.
      MethodToken myMethodToken =
                        myModuleBuilder.GetMethodToken(myMethodInfo);
      outputBlock.Text += String.Format(
         "Token used to identify the method of 'myType' within the module is {0:x}\n",
         myMethodToken.Token);

      object[] args = { "World!" };
      object myObject = Activator.CreateInstance(myType);
      outputBlock.Text += "Return value from MyMethod(\"World!\"): " +
         myMethodInfo.Invoke(myObject, args);
   }
}

/* This example produces output similar to the following:

Token used to identify the method of 'myType' within the module is 6000001
Return value from MyMethod("World!"): Hello, World!
 */


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft