TypeBuilder.DefineConstructor メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
動的な型に新しいコンストラクターを追加します。
オーバーロード
| 名前 | 説明 |
|---|---|
| DefineConstructor(MethodAttributes, CallingConventions, Type[]) |
指定された属性とシグネチャを持つ新しいコンストラクターを型に追加します。 |
| DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) |
指定された属性、シグネチャ、およびカスタム修飾子を使用して、新しいコンストラクターを型に追加します。 |
DefineConstructor(MethodAttributes, CallingConventions, Type[])
指定された属性とシグネチャを持つ新しいコンストラクターを型に追加します。
public:
System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type()) As ConstructorBuilder
パラメーター
- attributes
- MethodAttributes
コンストラクターの属性。
- callingConvention
- CallingConventions
コンストラクターの呼び出し規約。
- parameterTypes
- Type[]
コンストラクターのパラメーター型。
返品
定義されたコンストラクター。
- 属性
例外
この型は、以前に CreateType() を使用して作成されました。
例
次のコード サンプルでは、 DefineConstructor を使用して、コンストラクターの特定のシグネチャと属性を動的型に設定し、MSIL 作成に対応する ConstructorBuilder を返す方法を示します。
// Define the constructor.
Type[] constructorArgs = { typeof(String) };
ConstructorBuilder myConstructorBuilder =
helloWorldTypeBuilder.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, constructorArgs);
// Generate IL for the method. The constructor stores its argument in the private field.
ILGenerator myConstructorIL = myConstructorBuilder.GetILGenerator();
myConstructorIL.Emit(OpCodes.Ldarg_0);
myConstructorIL.Emit(OpCodes.Ldarg_1);
myConstructorIL.Emit(OpCodes.Stfld, myGreetingField);
myConstructorIL.Emit(OpCodes.Ret);
' Define the constructor.
Dim constructorArgs As Type() = {GetType(String)}
Dim myConstructorBuilder As ConstructorBuilder = helloWorldTypeBuilder.DefineConstructor _
(MethodAttributes.Public, CallingConventions.Standard, constructorArgs)
' Generate IL for the method. The constructor stores its argument in the private field.
Dim myConstructorIL As ILGenerator = myConstructorBuilder.GetILGenerator()
myConstructorIL.Emit(OpCodes.Ldarg_0)
myConstructorIL.Emit(OpCodes.Ldarg_1)
myConstructorIL.Emit(OpCodes.Stfld, myGreetingField)
myConstructorIL.Emit(OpCodes.Ret)
注釈
動的型のコンストラクターを定義しない場合は、パラメーターなしのコンストラクターが自動的に提供され、基底クラスのパラメーターなしのコンストラクターが呼び出されます。
動的型のコンストラクターを定義した場合、パラメーターなしのコンストラクターは提供されません。 定義したコンストラクターに加えて、パラメーターなしのコンストラクターを指定するには、次のオプションがあります。
基底クラスのパラメーターなしのコンストラクターを単に呼び出すパラメーターなしのコンストラクターが必要な場合は、 DefineDefaultConstructor メソッドを使用して作成できます (必要に応じてアクセスを制限することもできます)。 このパラメーターなしのコンストラクターの実装は提供しないでください。 その場合、コンストラクターを使用しようとすると例外がスローされます。 CreateType メソッドが呼び出されたときに例外はスローされません。
単に基底クラスのパラメーターなしのコンストラクターを呼び出す以上のことを行う、または基底クラスの別のコンストラクターを呼び出す、または他の何かを行うパラメーターなしのコンストラクターが必要な場合は、 TypeBuilder.DefineConstructor メソッドを使用して作成し、独自の実装を提供する必要があります。
適用対象
DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])
指定された属性、シグネチャ、およびカスタム修飾子を使用して、新しいコンストラクターを型に追加します。
public:
System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ requiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ optionalCustomModifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type(), requiredCustomModifiers As Type()(), optionalCustomModifiers As Type()()) As ConstructorBuilder
パラメーター
- attributes
- MethodAttributes
コンストラクターの属性。
- callingConvention
- CallingConventions
コンストラクターの呼び出し規約。
- parameterTypes
- Type[]
コンストラクターのパラメーター型。
- requiredCustomModifiers
- Type[][]
型の配列の配列。 型の各配列は、 IsConstなど、対応するパラメーターに必要なカスタム修飾子を表します。 特定のパラメーターに必要なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも必要なカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。
- optionalCustomModifiers
- Type[][]
型の配列の配列。 型の各配列は、 IsConstなど、対応するパラメーターの省略可能なカスタム修飾子を表します。 特定のパラメーターに省略可能なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 省略可能なカスタム修飾子を持つパラメーターがない場合は、配列の配列の代わりに null を指定します。
返品
定義されたコンストラクター。
- 属性
例外
requiredCustomModifiersまたはoptionalCustomModifiersのサイズがparameterTypesのサイズと等しくありません。
この型は、以前に CreateType() を使用して作成されました。
-または-
現在の動的な型の場合、 IsGenericType プロパティは trueされますが、 IsGenericTypeDefinition プロパティは false。
注釈
このオーバーロードは、マネージド コンパイラのデザイナーに提供されます。
Note
カスタム修飾子の詳細については、「ECMA C# と共通言語インフラストラクチャ標準」および「Standard ECMA-335 - 共通言語インフラストラクチャ (CLI)」を参照してください。