ModuleBuilder Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines and represents a module in a dynamic assembly.

Inheritance Hierarchy

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

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public Class ModuleBuilder _
    Inherits Module
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public class ModuleBuilder : Module

The ModuleBuilder type exposes the following members.

Properties

  Name Description
Public property Assembly Gets the dynamic assembly that defined this instance of ModuleBuilder. (Overrides Module.Assembly.)
Public property MetadataToken Gets a token that identifies the current dynamic module in metadata. (Overrides Module.MetadataToken.)
Public property Name A string that indicates that this is an in-memory module. (Overrides Module.Name.)

Top

Methods

  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 code.
Public method DefineEnum Defines an enumeration type in this module.
Public method DefineGlobalMethod(String, MethodAttributes, Type, array<Type[]) Defines a global method with the specified name, attributes, return type, and parameter types.
Public method DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, array<Type[]) Defines a global method with the specified name, attributes, calling convention, return type, and parameter types.
Public method DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, array<Type[], array<Type[], array<Type[], array<array<Type[][], array<array<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 DefineType(String) Defines a private type with the specified name in this module.
Public method DefineType(String, TypeAttributes) Defines a type with the specified type name and attributes in this module.
Public method DefineType(String, TypeAttributes, Type) Defines a type with the specified type name, attributes, and base type in this module.
Public method DefineType(String, TypeAttributes, Type, array<Type[]) Defines a type with the specified type name, attributes, base type, and implemented interfaces in this module.
Public method Equals Returns a value that indicates whether this instance is equal to the specified object. (Overrides Object.Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
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 GetField(String) Returns the global field that has 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 Object.GetHashCode().)
Public method GetMethod(String) Returns the global method that has the specified name. (Inherited from Module.)
Public method GetMethod(String, array<Type[]) Returns the global method that matches the specified name and parameter types. (Inherited from Module.)
Public method GetMethod(String, BindingFlags, Binder, CallingConventions, array<Type[], array<ParameterModifier[]) Returns the global method that has 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, array<Type[], array<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 GetSignatureToken(SignatureHelper) Defines a token for the signature that is defined by the specified SignatureHelper.
Public method GetSignatureToken(array<Byte[], Int32) Defines a token for the signature that has the specified character array and signature length.
Public method GetStringConstant Returns the token of the given string in the module’s constant pool.
Public method GetType() Gets the Type of the current instance. (Inherited from Object.)
Public method GetType(String) Gets the named type defined in this module. (Overrides Module.GetType(String).)
Public method GetType(String, Boolean) Gets the named type defined in this 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 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 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 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, array<Type[], array<Type[]) Returns the field identified by the specified metadata token, in the context defined by the specified generic type parameters. (Overrides Module.ResolveField(Int32, array<Type[], array<Type[]).)
Public method ResolveMethod(Int32) Returns the method or constructor that is identified by the specified metadata token. (Inherited from Module.)
Public method ResolveMethod(Int32, array<Type[], array<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, array<Type[], array<Type[]).)
Public method ResolveString Returns the string identified by the specified metadata token. (Overrides Module.ResolveString(Int32).)
Public method ResolveType(Int32) Returns the type that is identified by the specified metadata token. (Inherited from Module.)
Public method ResolveType(Int32, array<Type[], array<Type[]) Returns the type identified by the specified metadata token, in the context defined by the specified generic type parameters. (Overrides Module.ResolveType(Int32, array<Type[], array<Type[]).)
Public method SetCustomAttribute Applies a custom attribute to this module by using a custom attribute builder.
Public method ToString Returns the name of the module. (Inherited from Module.)

Top

Remarks

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

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

Examples

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

Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Get the current application domain for the current thread.
      Dim myCurrentDomain As AppDomain = AppDomain.CurrentDomain
      Dim myAssemblyName As New AssemblyName("TempAssembly")

      ' Define a dynamic assembly in the current application domain.
      Dim myAssemblyBuilder As AssemblyBuilder = _
               myCurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)

      ' Define a dynamic module in this assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")

      ' Define a runtime class with specified name and attributes.
      Dim myTypeBuilder As TypeBuilder = _
               myModuleBuilder.DefineType("TempClass", TypeAttributes.Public)

      ' Add 'Greeting' field to the class, with the specified attribute and type.
      Dim greetingField As FieldBuilder = _
               myTypeBuilder.DefineField("Greeting", GetType(String), FieldAttributes.Public)

      Dim myMethodArgs As Type() = {GetType(String)}

      ' Add 'MyMethod' method to the class, with the specified attribute and signature.
      Dim myMethod As MethodBuilder = _
               myTypeBuilder.DefineMethod("MyMethod", MethodAttributes.Public, _
               CallingConventions.Standard, GetType(String), myMethodArgs)

      ' Method to concatenate two strings.
      Dim stringConcat As MethodInfo = GetType(String).GetMethod("Concat", _
         BindingFlags.Static Or BindingFlags.Public, Type.DefaultBinder, _
         New Type() { GetType(String), GetType(String) }, _
         Nothing)

      Dim methodIL As ILGenerator = myMethod.GetILGenerator()
      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)

      myTypeBuilder.CreateType()

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

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

      Dim args As Object() = {"World!"}
      Dim myObject As Object = Activator.CreateInstance(myType)
      outputBlock.Text &= "Return value from MyMethod(""World!""): " & _
         myMethodInfo.Invoke(myObject, args)
   End Sub 
End Class 

' 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!
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!
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.