次の方法で共有


ModuleBuilder クラス

モジュールを定義および表現します。 DefineDynamicModule を呼び出して、ModuleBuilder のインスタンスを取得します。

この型のすべてのメンバの一覧については、ModuleBuilder メンバ を参照してください。

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

Public Class ModuleBuilder
   Inherits Module
[C#]
public class ModuleBuilder : Module
[C++]
public __gc class ModuleBuilder : public Module
[JScript]
public class ModuleBuilder extends Module

スレッドセーフ

Reflection Emit は、Boolean パラメータ isSynchronizedtrue に設定して呼び出した AppDomain.DefineDynamicAssembly メソッドで作成されたアセンブリを使用する場合は、スレッド セーフです。

使用例

[Visual Basic, C#, C++] 次のコード例は、 ModuleBuilder を使用して、動的モジュールを作成する方法を示しています。ModuleBuilder は、コンストラクタではなく、 AssemblyBuilderDefineDynamicModule の呼び出しによって作成されます。

 
Imports System
Imports System.Reflection
Imports System.Reflection.Emit

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
   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

[C#] 
using System;
using System.Reflection;
using System.Reflection.Emit;

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

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

public __gc 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 = S"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(S"TempModule");

      // Define a runtime class with specified name and attributes.
      TypeBuilder* myTypeBuilder =
         myModuleBuilder->DefineType(S"TempClass",TypeAttributes::Public);

      // Add 'Greeting' field to the class, with the specified attribute and type.
      FieldBuilder* greetingField =
         myTypeBuilder->DefineField(S"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(
         S"MyMethod",
         MethodAttributes::Public,
         CallingConventions::Standard,
         0,
         myMethodArgs);

      ILGenerator* methodIL = myMethod->GetILGenerator();
      methodIL->EmitWriteLine(S"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:
   __property AssemblyBuilder* get_MyAssembly()
   {
      return this->myAssemblyBuilder;
   }
};

int 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(S"TempModule");
   Console::WriteLine(S"The fully qualified name and path to this module is :{0}",
      myModuleBuilder->FullyQualifiedName );
   Type* myType = myModuleBuilder->GetType(S"TempClass");
   MethodInfo* myMethodInfo = 
      myType->GetMethod(S"MyMethod");
   // Get the token used to identify the method within this module.
   MethodToken myMethodToken = 
      myModuleBuilder->GetMethodToken(myMethodInfo);
   Console::WriteLine(S"Token used to identify the method of 'myType'"
      S" within the module is {0:x}", __box(myMethodToken.Token));
   Object* args[]={S"Hello."};
   Object* myObject = Activator::CreateInstance(myType,0,0);
   myMethodInfo->Invoke(myObject,args);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Reflection.Emit

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

ModuleBuilder メンバ | System.Reflection.Emit 名前空間