TypeBuilder.DefineTypeInitializer 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
이 형식에 대한 이니셜라이저를 정의합니다.
public:
System::Reflection::Emit::ConstructorBuilder ^ DefineTypeInitializer();
public System.Reflection.Emit.ConstructorBuilder DefineTypeInitializer ();
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineTypeInitializer ();
member this.DefineTypeInitializer : unit -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineTypeInitializer : unit -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineTypeInitializer () As ConstructorBuilder
반환
형식 이니셜라이저를 반환합니다.
- 특성
예외
포함하는 형식은 CreateType()을 사용하여 이전에 만든 것입니다.
예제
다음 코드 샘플에서는 를 사용하여 DefineTypeInitializer
초기화 생성자를 만드는 방법을 보여 줍니다.
// 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()
{
// Create the "HelloWorld" class
Type helloWorldType = CreateType();
Console.WriteLine("Full Name : " + helloWorldType.FullName);
Console.WriteLine("Static constructors:");
ConstructorInfo[] info =
helloWorldType.GetConstructors(BindingFlags.Static | BindingFlags.NonPublic);
for(int index=0; index < info.Length; index++)
Console.WriteLine(info[index].ToString());
// Print value stored in the static field
Console.WriteLine(helloWorldType.GetField("Greeting").GetValue(null));
Activator.CreateInstance(helloWorldType);
}
// Create the dynamic type.
private static Type CreateType()
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";
// Create the callee dynamic assembly.
AssemblyBuilder myAssembly = AssemblyBuilder.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 public static string field named "Greeting" in the type.
FieldBuilder greetingField = helloWorldClass.DefineField("Greeting", typeof(String),
FieldAttributes.Static | FieldAttributes.Public);
// Create the static constructor.
ConstructorBuilder constructor = helloWorldClass.DefineTypeInitializer();
// Generate IL for the method.
// The constructor stores its "Hello emit!" in the public field.
ILGenerator constructorIL = constructor.GetILGenerator();
constructorIL.Emit(OpCodes.Ldstr, "Hello emit!");
constructorIL.Emit(OpCodes.Stsfld, greetingField);
constructorIL.Emit(OpCodes.Ret);
return helloWorldClass.CreateType();
}
}
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
' 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
설명
만든 이니셜라이저는 항상 공용입니다.
적용 대상
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET