この型の初期化子を定義します。
Public Function DefineTypeInitializer() As ConstructorBuilder
[C#]
public ConstructorBuilder DefineTypeInitializer();
[C++]
public: ConstructorBuilder* DefineTypeInitializer();
[JScript]
public function DefineTypeInitializer() : ConstructorBuilder;
戻り値
型初期化子を返します。
例外
例外の種類 | 条件 |
---|---|
InvalidOperationException | 格納している型が、 CreateType を使用して既に作成されています。 |
解説
作成される初期化子は、常にパブリックです。
使用例
[Visual Basic, C#, C++] 次のコード例は、 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
[C#]
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);
}
}
[C++]
// Create the callee transient dynamic assembly.
TypeBuilder* CreateCallee(AppDomain* myDomain)
{
AssemblyName* myAssemblyName = new AssemblyName();
myAssemblyName->Name = S"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(S"EmittedModule");
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder* helloWorldClass = myModule->DefineType(S"HelloWorld", TypeAttributes::Public);
// Define a private String field named "Greeting" in the type.
FieldBuilder* greetingField = helloWorldClass->DefineField(S"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);
}
int main()
{
// Create the "HelloWorld" class
TypeBuilder* helloWorldClass = CreateCallee(Thread::GetDomain());
Console::WriteLine(S"Full Name : {0}", helloWorldClass->FullName);
Console::WriteLine(S"Constructors :");
ConstructorInfo* info[] =
helloWorldClass->GetConstructors(static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance));
for(int index=0; index < info->Length; index++)
Console::WriteLine(info[index]);
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
TypeBuilder クラス | TypeBuilder メンバ | System.Reflection.Emit 名前空間