This topic has not yet been rated - Rate this topic

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)]
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class ModuleBuilder : Module, 
	_ModuleBuilder

The ModuleBuilder type exposes the following members.

  Name Description
Public property Assembly Gets the dynamic assembly that defined this instance of ModuleBuilder. (Overrides Module.Assembly.)
Public property FullyQualifiedName Gets a String representing the fully qualified name and path to this module. (Overrides Module.FullyQualifiedName.)
Public property MDStreamVersion Gets the metadata stream version. (Overrides Module.MDStreamVersion.)
Public property MetadataToken Gets a token that identifies the current dynamic module in metadata. (Overrides Module.MetadataToken.)
Public property ModuleHandle Gets a handle for the module. (Inherited from Module.)
Public property ModuleVersionId Gets a universally unique identifier (UUID) that can be used to distinguish between two versions of a module. (Overrides Module.ModuleVersionId.)
Public property Name A string that indicates that this is an in-memory module. (Overrides Module.Name.)
Public property ScopeName Gets a string that represents the name of the dynamic module. (Overrides Module.ScopeName.)
Top
  Name Description
Public method CreateGlobalFunctions Completes the global function definitions and global data definitions for this dynamic module.
Public method DefineDocument Defines a document for source.
Public method DefineEnum Defines an enumeration type that is a value type with a single non-static field called value__ of the specified type.
Public method DefineGlobalMethod(String, MethodAttributes, Type, Type[]) Defines a global method with the specified name, attributes, return type, and parameter types.
Public method DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[]) Defines a global method with the specified name, attributes, calling convention, return type, and parameter types.
Public method DefineGlobalMethod(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 method DefineInitializedData Defines an initialized data field in the .sdata section of the portable executable (PE) file.
Public method DefineManifestResource Defines a binary large object (BLOB) that represents a manifest resource to be embedded in the dynamic assembly.
Public method DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.
Public method DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.
Public method DefineResource(String, String) Defines the named managed embedded resource to be stored in this module.
Public method DefineResource(String, String, ResourceAttributes) Defines the named managed embedded resource with the given attributes that is to be stored in this module.
Public method DefineType(String) Constructs a TypeBuilder for a private type with the specified name in this module.
Public method DefineType(String, TypeAttributes) Constructs a TypeBuilder given the type name and the type attributes.
Public method DefineType(String, TypeAttributes, Type) Constructs a TypeBuilder given type name, its attributes, and the type that the defined type extends.
Public method DefineType(String, TypeAttributes, Type, Int32) Constructs a TypeBuilder given the type name, the attributes, the type that the defined type extends, and the total size of the type.
Public method DefineType(String, TypeAttributes, Type, PackingSize) Constructs a TypeBuilder given the type name, the attributes, the type that the defined type extends, and the packing size of the type.
Public method DefineType(String, TypeAttributes, Type, Type[]) Constructs a TypeBuilder given the type name, attributes, the type that the defined type extends, and the interfaces that the defined type implements.
Public method DefineType(String, TypeAttributes, Type, PackingSize, Int32) Constructs a TypeBuilder given the type name, attributes, the type that the defined type extends, the packing size of the defined type, and the total size of the defined type.
Public method DefineUninitializedData Defines an uninitialized data field in the .sdata section of the portable executable (PE) file.
Public method DefineUnmanagedResource(Byte[]) Defines an unmanaged embedded resource given an opaque binary large object (BLOB) of bytes.
Public method DefineUnmanagedResource(String) Defines an unmanaged resource given the name of Win32 resource file.
Public method Equals Returns a value that indicates whether this instance is equal to the specified object. (Overrides Module.Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method FindTypes Returns an array of classes accepted by the given filter and filter criteria. (Inherited from Module.)
Public method GetArrayMethod Returns the named method on an array class.
Public method GetArrayMethodToken Returns the token for the named method on an array class.
Public method GetConstructorToken Returns the token used to identify the specified constructor within this module.
Public method GetCustomAttributes(Boolean) Returns all the custom attributes that have been applied to the current ModuleBuilder. (Overrides Module.GetCustomAttributes(Boolean).)
Public method GetCustomAttributes(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 method GetCustomAttributesData Returns information about the attributes that have been applied to the current ModuleBuilder, expressed as CustomAttributeData objects. (Overrides Module.GetCustomAttributesData().)
Public method GetField(String) Returns a field having the specified name. (Inherited from Module.)
Public method GetField(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 method GetFields() Returns the global fields defined on the module. (Inherited from Module.)
Public method GetFields(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 method GetFieldToken Returns the token used to identify the specified field within this module.
Public method GetHashCode Returns the hash code for this instance. (Overrides Module.GetHashCode().)
Public method GetMethod(String) Returns a method having the specified name. (Inherited from Module.)
Public method GetMethod(String, Type[]) Returns a method having the specified name and parameter types. (Inherited from Module.)
Public method GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) Returns a method having the specified name, binding information, calling convention, and parameter types and modifiers. (Inherited from Module.)
Protected method GetMethodImpl Returns the module-level method that matches the specified criteria. (Overrides Module.GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]).)
Public method GetMethods() Returns the global methods defined on the module. (Inherited from Module.)
Public method GetMethods(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 method GetMethodToken Returns the token used to identify the specified method within this module.
Public method GetObjectData Provides an ISerializable implementation for serialized objects. (Inherited from Module.)
Public method GetPEKind Gets a pair of values indicating the nature of the code in a module and the platform targeted by the module. (Overrides Module.GetPEKind(PortableExecutableKinds, ImageFileMachine).)
Public method GetSignatureToken(SignatureHelper) Defines a token for the signature that is defined by the specified SignatureHelper.
Public method GetSignatureToken(Byte[], Int32) Defines a token for the signature that has the specified character array and signature length.
Public method GetSignerCertificate Returns an X509Certificate object corresponding to the certificate included in the Authenticode signature of the assembly which this module belongs to. If the assembly has not been Authenticode signed, null is returned. (Overrides Module.GetSignerCertificate().)
Public method GetStringConstant Returns the token of the given string in the module’s constant pool.
Public method GetSymWriter Returns the symbol writer associated with this dynamic module.
Public method GetType() Gets the Type of the current instance. (Inherited from Object.)
Public method GetType(String) Gets the named type defined in the module. (Overrides Module.GetType(String).)
Public method GetType(String, Boolean) Gets the named type defined in the module, optionally ignoring the case of the type name. (Overrides Module.GetType(String, Boolean).)
Public method GetType(String, Boolean, Boolean) Gets the named type defined in the 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 method GetTypes Returns all the classes defined within this module. (Overrides Module.GetTypes().)
Public method GetTypeToken(String) Returns the token used to identify the type with the specified name.
Public method GetTypeToken(Type) Returns the token used to identify the specified type within this module.
Public method IsDefined Returns a value that indicates whether the specified attribute type has been applied to this module. (Overrides Module.IsDefined(Type, Boolean).)
Public method IsResource Gets a value indicating whether the object is a resource. (Overrides Module.IsResource().)
Public method IsTransient Returns a value that indicates whether this dynamic module is transient.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ResolveField(Int32) Returns the field identified by the specified metadata token. (Inherited from Module.)
Public method ResolveField(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 method ResolveMember(Int32) Returns the type or member identified by the specified metadata token. (Inherited from Module.)
Public method ResolveMember(Int32, Type[], Type[]) Returns the type or member identified by the specified metadata token, in the context defined by the specified generic type parameters. (Overrides Module.ResolveMember(Int32, Type[], Type[]).)
Public method ResolveMethod(Int32) Returns the method or constructor identified by the specified metadata token. (Inherited from Module.)
Public method ResolveMethod(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 method ResolveSignature Returns the signature blob identified by a metadata token. (Overrides Module.ResolveSignature(Int32).)
Public method ResolveString Returns the string identified by the specified metadata token. (Overrides Module.ResolveString(Int32).)
Public method ResolveType(Int32) Returns the type identified by the specified metadata token. (Inherited from Module.)
Public method ResolveType(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 method SetCustomAttribute(CustomAttributeBuilder) Applies a custom attribute to this module by using a custom attribute builder.
Public method SetCustomAttribute(ConstructorInfo, Byte[]) Applies a custom attribute to this module by using a specified binary large object (BLOB) that represents the attribute.
Public method SetSymCustomAttribute This method does nothing.
Public method SetUserEntryPoint Sets the user entry point.
Public method ToString Returns the name of the module. (Inherited from Module.)
Top
  Name Description
Explicit interface implemetation Private method _Module.GetIDsOfNames Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Module.)
Explicit interface implemetation Private method _Module.GetTypeInfo Retrieves the type information for an object, which can then be used to get the type information for an interface. (Inherited from Module.)
Explicit interface implemetation Private method _Module.GetTypeInfoCount Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Module.)
Explicit interface implemetation Private method _Module.Invoke Provides access to properties and methods exposed by an object. (Inherited from Module.)
Explicit interface implemetation Private method _ModuleBuilder.GetIDsOfNames For a description of this member, see _ModuleBuilder.GetIDsOfNames.
Explicit interface implemetation Private method _ModuleBuilder.GetTypeInfo For a description of this member, see _ModuleBuilder.GetTypeInfo.
Explicit interface implemetation Private method _ModuleBuilder.GetTypeInfoCount For a description of this member, see _ModuleBuilder.GetTypeInfoCount.
Explicit interface implemetation Private method _ModuleBuilder.Invoke For a description of this member, see _ModuleBuilder.Invoke.
Top

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

Note Note

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code sample demonstrates the use of ModuleBuilder to create a dynamic module. Note that the ModuleBuilder is created by calling DefineDynamicModule in AssemblyBuilder, rather than through a constructor.


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);
   }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ