TypeBuilder.DefineTypeInitializer メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > TypeBuilder.DefineTypeInitializer メソッドの意味・解説 

TypeBuilder.DefineTypeInitializer メソッド

この型の初期化子定義します

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Function DefineTypeInitializer As
 ConstructorBuilder
Dim instance As TypeBuilder
Dim returnValue As ConstructorBuilder

returnValue = instance.DefineTypeInitializer
[ComVisibleAttribute(true)] 
public ConstructorBuilder DefineTypeInitializer ()
[ComVisibleAttribute(true)] 
public:
ConstructorBuilder^ DefineTypeInitializer ()
/** @attribute ComVisibleAttribute(true) */ 
public ConstructorBuilder DefineTypeInitializer ()
ComVisibleAttribute(true) 
public function DefineTypeInitializer () :
 ConstructorBuilder

戻り値
初期化子返します

例外例外
例外種類条件

InvalidOperationException

格納している型が、CreateType を使用して既に作成されています。

解説解説
使用例使用例

次のコード例は、DefineTypeInitializer使用して初期化コンストラクタ作成する方法示してます。

Public Class MyApplication

   Public Shared Sub Main()
      ' Create the "HelloWorld" class
      Dim helloWorldClass As TypeBuilder =
 CreateCallee(Thread.GetDomain())
      Console.WriteLine("Full Name : " + helloWorldClass.FullName)
      Console.WriteLine("Constructors :")
      Dim info As ConstructorInfo() = helloWorldClass.GetConstructors(BindingFlags.Public
 Or _
                                                                     BindingFlags.Instance)
      Dim index As Integer
      For index = 0 To info.Length - 1
         Console.WriteLine(info(index).ToString())
      Next index
   End Sub 'Main

   ' Create the callee transient dynamic assembly.
   Private Shared Function
 CreateCallee(myDomain As AppDomain) As TypeBuilder
      Dim myAssemblyName As New
 AssemblyName()
      myAssemblyName.Name = "EmittedAssembly"

      ' Create the callee dynamic assembly.
      Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAssemblyName,
 _
                                                               AssemblyBuilderAccess.Run)
      ' Create a dynamic module named "CalleeModule" in the
 callee assembly.
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")

      ' Define a public class named "HelloWorld" in the assembly.
      Dim helloWorldClass As TypeBuilder =
 myModule.DefineType("HelloWorld", TypeAttributes.Public)
      ' Define a private String field named "Greeting" in
 the type.
      Dim greetingField As FieldBuilder = helloWorldClass.DefineField("Greeting",
 GetType(String), _
                                                                     FieldAttributes.Private)

      ' Create the constructor.
      Dim constructor As ConstructorBuilder
 = helloWorldClass.DefineTypeInitializer()

      ' Generate IL for the method. The constructor calls its base class
      ' constructor. The constructor stores its argument in the private
 field.
      Dim constructorIL As ILGenerator = constructor.GetILGenerator()
      constructorIL.Emit(OpCodes.Ldarg_0)
      Dim superConstructor As ConstructorInfo
 = GetType(Object).GetConstructor(New
 Type() {})
      constructorIL.Emit(OpCodes.Call, superConstructor)
      constructorIL.Emit(OpCodes.Ldarg_0)
      constructorIL.Emit(OpCodes.Ldarg_1)
      constructorIL.Emit(OpCodes.Stfld, greetingField)
      constructorIL.Emit(OpCodes.Ret)

      helloWorldClass.CreateType()
      Return helloWorldClass
   End Function 'CreateCallee
End Class 'MyApplication
public class MyApplication
{
   public static void Main()
   {
      // Create the "HelloWorld" class
      TypeBuilder helloWorldClass = CreateCallee(Thread.GetDomain());
      Console.WriteLine("Full Name : " + helloWorldClass.FullName);
      Console.WriteLine("Constructors :");
      ConstructorInfo[] info =
         helloWorldClass.GetConstructors(BindingFlags.Public|BindingFlags.Instance);
      for(int index=0; index < info.Length;
 index++)
         Console.WriteLine(info[index].ToString());
   }

   // Create the callee transient dynamic assembly.
   private static TypeBuilder CreateCallee(AppDomain
 myDomain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";

      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(myAssemblyName
,
         AssemblyBuilderAccess.Run);
      // Create a dynamic module named "CalleeModule" in the
 callee assembly.
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");

      // Define a public class named "HelloWorld" in the assembly.
      TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);
      // Define a private String field named "Greeting" in
 the type.
      FieldBuilder greetingField = helloWorldClass.DefineField("Greeting",
 typeof(String),
         FieldAttributes.Private);

      // Create the constructor.
      ConstructorBuilder constructor = helloWorldClass.DefineTypeInitializer();

      // Generate IL for the method. The constructor calls its base
 class
      // constructor. The constructor stores its argument in the private
 field.
      ILGenerator constructorIL = constructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(Object).GetConstructor(new
 Type[0]);
      constructorIL.Emit(OpCodes.Call, superConstructor);
      constructorIL.Emit(OpCodes.Ldarg_0);
      constructorIL.Emit(OpCodes.Ldarg_1);
      constructorIL.Emit(OpCodes.Stfld, greetingField);
      constructorIL.Emit(OpCodes.Ret);

      helloWorldClass.CreateType();
      return(helloWorldClass);
   }
}
// Create the callee transient dynamic assembly.
TypeBuilder^ CreateCallee( AppDomain^ myDomain )
{
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "EmittedAssembly";
   
   // Create the callee dynamic assembly.
   AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly( myAssemblyName,
 AssemblyBuilderAccess::Run );
   
   // Create a dynamic module named "CalleeModule" in the
 callee assembly.
   ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule"
 );
   
   // Define a public class named "HelloWorld" in the assembly.
   TypeBuilder^ helloWorldClass = myModule->DefineType( "HelloWorld",
 TypeAttributes::Public );
   
   // Define a private String field named "Greeting" in the
 type.
   FieldBuilder^ greetingField = helloWorldClass->DefineField( "Greeting",
 String::typeid, FieldAttributes::Private );
   
   // Create the constructor.
   ConstructorBuilder^ constructor = helloWorldClass->DefineTypeInitializer();
   
   // Generate IL for the method. The constructor calls its base class
   // constructor. The constructor stores its argument in the private
 field.
   ILGenerator^ constructorIL = constructor->GetILGenerator();
   constructorIL->Emit( OpCodes::Ldarg_0 );
   ConstructorInfo^ superConstructor = Object::typeid->GetConstructor( gcnew array<Type^>(0)
 );
   constructorIL->Emit( OpCodes::Call, superConstructor );
   constructorIL->Emit( OpCodes::Ldarg_0 );
   constructorIL->Emit( OpCodes::Ldarg_1 );
   constructorIL->Emit( OpCodes::Stfld, greetingField );
   constructorIL->Emit( OpCodes::Ret );
   helloWorldClass->CreateType();
   return (helloWorldClass);
}

int main()
{
   // Create the "HelloWorld" class
   TypeBuilder^ helloWorldClass = CreateCallee( Thread::GetDomain() );
   Console::WriteLine( "Full Name : {0}", helloWorldClass->FullName
 );
   Console::WriteLine( "Constructors :" );
   array<ConstructorInfo^>^info = helloWorldClass->GetConstructors( static_cast<BindingFlags>(BindingFlags::Public
 | BindingFlags::Instance) );
   for ( int index = 0; index < info->Length;
 index++ )
      Console::WriteLine( info[ index ] );
}
public class MyApplication
{
    public static void main(String[]
 args)
    {
        // Create the "HelloWorld" class
        TypeBuilder helloWorldClass =
            CreateCallee(System.Threading.Thread.GetDomain());
        Console.WriteLine("Full Name : " + helloWorldClass.get_FullName());
        Console.WriteLine("Constructors :");
        ConstructorInfo info[] =
            helloWorldClass.GetConstructors(BindingFlags.Public
            | BindingFlags.Instance);
        for (int index = 0; index < info.length;
 index++) {
            Console.WriteLine(info.get_Item(index).ToString());
        }
    } //main

    // Create the callee transient dynamic assembly.
    private static TypeBuilder CreateCallee(AppDomain
 myDomain)
    {
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.set_Name("EmittedAssembly");
        // Create the callee dynamic assembly.
        AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
            myAssemblyName, AssemblyBuilderAccess.Run);
        // Create a dynamic module named "CalleeModule" in the
 callee assembly.
        ModuleBuilder myModule =
            myAssembly.DefineDynamicModule("EmittedModule");
        // Define a public class named "HelloWorld" in the
 assembly.
        TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld"
,
            TypeAttributes.Public);
        // Define a private String field named "Greeting" in
 the type.
        FieldBuilder greetingField = helloWorldClass.DefineField("Greeting"
,
            String.class.ToType(), FieldAttributes.Private);
        // Create the constructor.
        ConstructorBuilder constructor = helloWorldClass.DefineTypeInitializer();
        // Generate IL for the method. The constructor calls its base
 class
        // constructor. The constructor stores its argument in the private
 field.
        ILGenerator constructorIL = constructor.GetILGenerator();
        constructorIL.Emit(OpCodes.Ldarg_0);
        ConstructorInfo superConstructor = Object.class.ToType().
            GetConstructor(new Type[0]);
        constructorIL.Emit(OpCodes.Call, superConstructor);
        constructorIL.Emit(OpCodes.Ldarg_0);
        constructorIL.Emit(OpCodes.Ldarg_1);
        constructorIL.Emit(OpCodes.Stfld, greetingField);
        constructorIL.Emit(OpCodes.Ret);

        helloWorldClass.CreateType();
        return helloWorldClass;
    } //CreateCallee
} //MyApplication
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TypeBuilder クラス
TypeBuilder メンバ
System.Reflection.Emit 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からTypeBuilder.DefineTypeInitializer メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からTypeBuilder.DefineTypeInitializer メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からTypeBuilder.DefineTypeInitializer メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

TypeBuilder.DefineTypeInitializer メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TypeBuilder.DefineTypeInitializer メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS