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 | |
---|---|---|
Assembly | Gets the dynamic assembly that defined this instance of ModuleBuilder. (Overrides Module.Assembly.) | |
MetadataToken | Gets a token that identifies the current dynamic module in metadata. (Overrides Module.MetadataToken.) | |
Name | A string that indicates that this is an in-memory module. (Overrides Module.Name.) |
Top
Methods
Name | Description | |
---|---|---|
CreateGlobalFunctions | Completes the global function definitions and global data definitions for this dynamic module. | |
DefineDocument | Defines a document for source code. | |
DefineEnum | Defines an enumeration type in this module. | |
DefineGlobalMethod(String, MethodAttributes, Type, array<Type[]) | Defines a global method with the specified name, attributes, return type, and parameter types. | |
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, array<Type[]) | Defines a global method with the specified name, attributes, calling convention, return type, and parameter types. | |
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. | |
DefineType(String) | Defines a private type with the specified name in this module. | |
DefineType(String, TypeAttributes) | Defines a type with the specified type name and attributes in this module. | |
DefineType(String, TypeAttributes, Type) | Defines a type with the specified type name, attributes, and base type in this module. | |
DefineType(String, TypeAttributes, Type, array<Type[]) | Defines a type with the specified type name, attributes, base type, and implemented interfaces in this module. | |
Equals | Returns a value that indicates whether this instance is equal to the specified object. (Overrides Object.Equals(Object).) | |
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.) | |
GetArrayMethod | Returns the named method on an array class. | |
GetArrayMethodToken | Returns the token for the named method on an array class. | |
GetConstructorToken | Returns the token used to identify the specified constructor within this module. | |
GetCustomAttributes(Boolean) | Returns all the custom attributes that have been applied to the current ModuleBuilder. (Overrides Module.GetCustomAttributes(Boolean).) | |
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).) | |
GetField(String) | Returns the global field that has the specified name. (Inherited from Module.) | |
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).) | |
GetFields() | Returns the global fields defined on the module. (Inherited from Module.) | |
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).) | |
GetFieldToken | Returns the token used to identify the specified field within this module. | |
GetHashCode | Returns the hash code for this instance. (Overrides Object.GetHashCode().) | |
GetMethod(String) | Returns the global method that has the specified name. (Inherited from Module.) | |
GetMethod(String, array<Type[]) | Returns the global method that matches the specified name and parameter types. (Inherited from Module.) | |
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.) | |
GetMethodImpl | Returns the module-level method that matches the specified criteria. (Overrides Module.GetMethodImpl(String, BindingFlags, Binder, CallingConventions, array<Type[], array<ParameterModifier[]).) | |
GetMethods() | Returns the global methods defined on the module. (Inherited from Module.) | |
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).) | |
GetMethodToken | Returns the token used to identify the specified method within this module. | |
GetSignatureToken(SignatureHelper) | Defines a token for the signature that is defined by the specified SignatureHelper. | |
GetSignatureToken(array<Byte[], Int32) | Defines a token for the signature that has the specified character array and signature length. | |
GetStringConstant | Returns the token of the given string in the module’s constant pool. | |
GetType() | Gets the Type of the current instance. (Inherited from Object.) | |
GetType(String) | Gets the named type defined in this module. (Overrides Module.GetType(String).) | |
GetType(String, Boolean) | Gets the named type defined in this module, optionally ignoring the case of the type name. (Overrides Module.GetType(String, Boolean).) | |
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).) | |
GetTypes | Returns all the classes defined within this module. (Overrides Module.GetTypes().) | |
GetTypeToken(String) | Returns the token used to identify the type with the specified name. | |
GetTypeToken(Type) | Returns the token used to identify the specified type within this module. | |
IsDefined | Returns a value that indicates whether the specified attribute type has been applied to this module. (Overrides Module.IsDefined(Type, Boolean).) | |
IsTransient | Returns a value that indicates whether this dynamic module is transient. | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ResolveField(Int32) | Returns the field identified by the specified metadata token. (Inherited from Module.) | |
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[]).) | |
ResolveMethod(Int32) | Returns the method or constructor that is identified by the specified metadata token. (Inherited from Module.) | |
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[]).) | |
ResolveString | Returns the string identified by the specified metadata token. (Overrides Module.ResolveString(Int32).) | |
ResolveType(Int32) | Returns the type that is identified by the specified metadata token. (Inherited from Module.) | |
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[]).) | |
SetCustomAttribute | Applies a custom attribute to this module by using a custom attribute builder. | |
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.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
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.