ModuleBuilder 类

定义并表示模块。通过调用 DefineDynamicModule 获取 ModuleBuilder 的实例。

**命名空间:**System.Reflection.Emit
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public Class ModuleBuilder
    Inherits Module
    Implements _ModuleBuilder
用法
Dim instance As ModuleBuilder
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public class ModuleBuilder : Module, _ModuleBuilder
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
public ref class ModuleBuilder : public Module, _ModuleBuilder
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public class ModuleBuilder extends Module implements _ModuleBuilder
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public class ModuleBuilder extends Module implements _ModuleBuilder

备注

提示

应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:MayLeakOnAbortHostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

示例

下面的代码示例演示了如何使用 ModuleBuilder 创建动态模块。请注意 ModuleBuilder 是通过调用 AssemblyBuilder 中的 DefineDynamicModule 创建的,而不是通过构造函数创建的。

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Class CodeGenerator
   Private myAssemblyBuilder As AssemblyBuilder

   Public Sub New()
      ' Get the current application domain for the current thread.
      Dim myCurrentDomain As AppDomain = AppDomain.CurrentDomain
      Dim myAssemblyName As 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.
      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, Nothing, myMethodArgs)

      Dim methodIL As ILGenerator = 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()
   End Sub 'New

   Public ReadOnly Property MyAssembly() As AssemblyBuilder
      Get
         Return Me.myAssemblyBuilder
      End Get
   End Property
End Class 'CodeGenerator

Public Class TestClass
   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
      Dim myCodeGenerator As New CodeGenerator()
      ' Get the assembly builder for 'myCodeGenerator' object.
      Dim myAssemblyBuilder As AssemblyBuilder = myCodeGenerator.MyAssembly
      ' Get the module builder for the above assembly builder object .
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.GetDynamicModule("TempModule")
      Console.WriteLine("The fully qualified name and path to this " + _
                        "module is :" + myModuleBuilder.FullyQualifiedName)
      Dim myType As Type = myModuleBuilder.GetType("TempClass")
      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)
      Console.WriteLine("Token used to identify the method of 'myType'" + _
                        " within the module is {0:x}", myMethodToken.Token)
      Dim args As Object() = {"Hello."}
      Dim myObject As Object = Activator.CreateInstance(myType, Nothing, Nothing)
      myMethodInfo.Invoke(myObject, args)
   End Sub 'Main
End Class 'TestClass
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);
   }
}
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
public ref class CodeGenerator
{
private:
   AssemblyBuilder^ myAssemblyBuilder;

public:
   CodeGenerator()
   {
      // Get the current application domain for the current thread.
      AppDomain^ myCurrentDomain = AppDomain::CurrentDomain;
      AssemblyName^ myAssemblyName = gcnew 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", String::typeid, FieldAttributes::Public );
      array<Type^>^myMethodArgs = {String::typeid};

      // Add 'MyMethod' method to the class, with the specified attribute and signature.
      MethodBuilder^ myMethod = myTypeBuilder->DefineMethod( "MyMethod", MethodAttributes::Public, CallingConventions::Standard, nullptr, 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();
   }

   property AssemblyBuilder^ MyAssembly 
   {
      AssemblyBuilder^ get()
      {
         return this->myAssemblyBuilder;
      }
   }
};

int main()
{
   CodeGenerator^ myCodeGenerator = gcnew 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 :{0}", 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 );
   array<Object^>^args = {"Hello."};
   Object^ myObject = Activator::CreateInstance( myType, nullptr, nullptr );
   myMethodInfo->Invoke( myObject, args );
}

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ModuleBuilder 成员
System.Reflection.Emit 命名空间