다음을 통해 공유


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

플랫폼

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에서 지원

참고 항목

참조

TypeBuilder 클래스
TypeBuilder 멤버
System.Reflection.Emit 네임스페이스