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)

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class ModuleBuilder : Module, 
	_ModuleBuilder

The ModuleBuilder type exposes the following members.

  NameDescription
Public propertyAssemblyGets the dynamic assembly that defined this instance of ModuleBuilder. (Overrides Module.Assembly.)
Public propertyCustomAttributesGets a collection that contains this module's custom attributes. (Inherited from Module.)
Public propertyFullyQualifiedNameGets a String representing the fully qualified name and path to this module. (Overrides Module.FullyQualifiedName.)
Public propertyMDStreamVersionGets the metadata stream version. (Overrides Module.MDStreamVersion.)
Public propertyMetadataTokenGets a token that identifies the current dynamic module in metadata. (Overrides Module.MetadataToken.)
Public propertyModuleHandleGets a handle for the module. (Inherited from Module.)
Public propertyModuleVersionIdGets a universally unique identifier (UUID) that can be used to distinguish between two versions of a module. (Overrides Module.ModuleVersionId.)
Public propertyNameA string that indicates that this is an in-memory module. (Overrides Module.Name.)
Public propertyScopeNameGets a string that represents the name of the dynamic module. (Overrides Module.ScopeName.)
Top

  NameDescription
Public methodCreateGlobalFunctionsCompletes the global function definitions and global data definitions for this dynamic module.
Public methodDefineDocumentDefines a document for source.
Public methodDefineEnumDefines an enumeration type that is a value type with a single non-static field called value__ of the specified type.
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 methodDefineInitializedDataDefines an initialized data field in the .sdata section of the portable executable (PE) file.
Public methodDefineManifestResourceDefines a binary large object (BLOB) that represents a manifest resource to be embedded in the dynamic assembly.
Public methodDefinePInvokeMethod(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 methodDefinePInvokeMethod(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 methodDefineResource(String, String)Defines the named managed embedded resource to be stored in this module.
Public methodDefineResource(String, String, ResourceAttributes)Defines the named managed embedded resource with the given attributes that is to be stored in this module.
Public methodDefineType(String)Constructs a TypeBuilder for a private type with the specified name in this module.
Public methodDefineType(String, TypeAttributes)Constructs a TypeBuilder given the type name and the type attributes.
Public methodDefineType(String, TypeAttributes, Type)Constructs a TypeBuilder given type name, its attributes, and the type that the defined type extends.
Public methodDefineType(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 methodDefineType(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 methodDefineType(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 methodDefineType(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 methodDefineUninitializedDataDefines an uninitialized data field in the .sdata section of the portable executable (PE) file.
Public methodDefineUnmanagedResource(Byte[])Defines an unmanaged embedded resource given an opaque binary large object (BLOB) of bytes.
Public methodDefineUnmanagedResource(String)Defines an unmanaged resource given the name of Win32 resource file.
Public methodEqualsReturns a value that indicates whether this instance is equal to the specified object. (Overrides Module.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFindTypesReturns an array of classes accepted by the given filter and filter criteria. (Inherited from Module.)
Public methodGetArrayMethodReturns the named method on an array class.
Public methodGetArrayMethodTokenReturns the token for the named method on an array class.
Public methodGetConstructorToken(ConstructorInfo)Returns the token used to identify the specified constructor within this module.
Public methodGetConstructorToken(ConstructorInfo, IEnumerable<Type>)Returns the token used to identify the constructor that has the specified attributes and parameter types 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 methodGetCustomAttributesDataReturns information about the attributes that have been applied to the current ModuleBuilder, expressed as CustomAttributeData objects. (Overrides Module.GetCustomAttributesData().)
Public methodGetField(String)Returns a field having 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 Module.GetHashCode().)
Public methodGetMethod(String)Returns a method having the specified name. (Inherited from Module.)
Public methodGetMethod(String, Type[])Returns a method having the specified name and parameter types. (Inherited from Module.)
Public methodGetMethod(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 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 methodGetMethodToken(MethodInfo)Returns the token used to identify the specified method within this module.
Public methodGetMethodToken(MethodInfo, IEnumerable<Type>)Returns the token used to identify the method that has the specified attributes and parameter types within this module.
Public methodGetObjectDataProvides an ISerializable implementation for serialized objects. (Inherited from Module.)
Public methodGetPEKindGets 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 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 methodGetSignerCertificateReturns 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 methodGetStringConstantReturns the token of the given string in the module’s constant pool.
Public methodGetSymWriterReturns the symbol writer associated with this dynamic module.
Public methodGetType()Gets the Type of the current instance. (Inherited from Object.)
Public methodGetType(String)Gets the named type defined in the module. (Overrides Module.GetType(String).)
Public methodGetType(String, Boolean)Gets the named type defined in the 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 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 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 methodIsResourceGets a value indicating whether the object is a resource. (Overrides Module.IsResource().)
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 methodResolveMember(Int32)Returns the type or member identified by the specified metadata token. (Inherited from Module.)
Public methodResolveMember(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 methodResolveMethod(Int32)Returns the method or constructor 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 methodResolveSignatureReturns the signature blob identified by a metadata token. (Overrides Module.ResolveSignature(Int32).)
Public methodResolveStringReturns the string identified by the specified metadata token. (Overrides Module.ResolveString(Int32).)
Public methodResolveType(Int32)Returns the type 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 methodSetCustomAttribute(CustomAttributeBuilder)Applies a custom attribute to this module by using a custom attribute builder.
Public methodSetCustomAttribute(ConstructorInfo, Byte[])Applies a custom attribute to this module by using a specified binary large object (BLOB) that represents the attribute.
Public methodSetSymCustomAttributeThis method does nothing.
Public methodSetUserEntryPointSets the user entry point.
Public methodToStringReturns the name of the module. (Inherited from Module.)
Top

  NameDescription
Public Extension MethodGetCustomAttribute(Type)Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified module. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttribute<T>()Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified module. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes()Overloaded. Retrieves a collection of custom attributes that are applied to a specified module. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes(Type)Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified module. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes<T>()Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified module. (Defined by CustomAttributeExtensions.)
Public Extension MethodIsDefinedIndicates whether custom attributes of a specified type are applied to a specified module. (Defined by CustomAttributeExtensions.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Module.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Module.)
Explicit interface implemetationPrivate method_Module.GetTypeInfoRetrieves the type information for an object, which can then be used to get the type information for an interface. (Inherited from Module.)
Explicit interface implemetationPrivate method_Module.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Module.)
Explicit interface implemetationPrivate method_Module.InvokeProvides access to properties and methods exposed by an object. (Inherited from Module.)
Explicit interface implemetationPrivate method_ModuleBuilder.GetIDsOfNamesFor a description of this member, see _ModuleBuilder.GetIDsOfNames.
Explicit interface implemetationPrivate method_ModuleBuilder.GetTypeInfoFor a description of this member, see _ModuleBuilder.GetTypeInfo.
Explicit interface implemetationPrivate method_ModuleBuilder.GetTypeInfoCountFor a description of this member, see _ModuleBuilder.GetTypeInfoCount.
Explicit interface implemetationPrivate method_ModuleBuilder.InvokeFor a description of this member, see _ModuleBuilder.Invoke.
Top

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

NoteNote

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.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft