英語で読む

次の方法で共有


TypeBuilder.DefineConstructor メソッド

定義

動的な型に新しいコンストラクターを追加します。

オーバーロード

DefineConstructor(MethodAttributes, CallingConventions, Type[])

型に、指定した属性およびシグネチャの新しいコンストラクターを追加します。

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

指定された属性、署名、およびカスタム修飾子で、型に新しいコンストラクターを追加します。

DefineConstructor(MethodAttributes, CallingConventions, Type[])

ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs

型に、指定した属性およびシグネチャの新しいコンストラクターを追加します。

C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes);
C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);

パラメーター

attributes
MethodAttributes

コンストラクターの属性。

callingConvention
CallingConventions

コンストラクターの呼び出し規約。

parameterTypes
Type[]

コンストラクターのパラメーター型。

戻り値

定義済みのコンストラクター。

属性

例外

型は CreateType() を使用して既に作成されました。

次のコード サンプルでは、 を使用 DefineConstructor して、コンストラクターの特定のシグネチャと属性を動的型に設定し、MSIL 作成に対応する ConstructorBuilder を返す方法を示します。

C#
// 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);

注釈

動的型のコンストラクターを定義しない場合、パラメーターなしのコンストラクターが自動的に提供され、基底クラスのパラメーターなしのコンストラクターが呼び出されます。

動的型のコンストラクターを定義した場合、パラメーターなしのコンストラクターは提供されません。 定義したコンストラクターに加えて、パラメーターなしのコンストラクターを提供するには、次のオプションがあります。

  • 基底クラスのパラメーターなしのコンストラクターを単に呼び出すパラメーターなしのコンストラクターが必要な場合は、 メソッドを DefineDefaultConstructor 使用して作成できます (必要に応じてアクセスを制限します)。 このパラメーターなしのコンストラクターの実装を提供しないでください。 その場合、コンストラクターを使用しようとすると例外がスローされます。 メソッドが呼び出されたときに CreateType 例外はスローされません。

  • 単に基底クラスのパラメーターなしのコンストラクターを呼び出す以上のことを行うパラメーターなしのコンストラクター、または基底クラスの別のコンストラクターを呼び出す、または他の何かを完全に行うパラメーターなしのコンストラクターが必要な場合は、 メソッドを TypeBuilder.DefineConstructor 使用してメソッドを作成し、独自の実装を提供する必要があります。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs

指定された属性、署名、およびカスタム修飾子で、型に新しいコンストラクターを追加します。

C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers);
C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
C#
[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);

パラメーター

attributes
MethodAttributes

コンストラクターの属性。

callingConvention
CallingConventions

コンストラクターの呼び出し規約。

parameterTypes
Type[]

コンストラクターのパラメーター型。

requiredCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、IsConst のような、対応するパラメーターの必須のカスタム修飾子を表します。 特定のパラメーターに必須のカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも必須のカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。

optionalCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、 IsConstのような、対応するパラメーターの省略可能なカスタム修飾子を表します。 特定のパラメーターに省略可能なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも省略可能なカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。

戻り値

定義済みのコンストラクター。

属性

例外

requiredCustomModifiers または optionalCustomModifiers のサイズは parameterTypes のサイズと同じではありません。

型は CreateType() を使用して既に作成されました。

- または -

現在の動的な型では、IsGenericType プロパティは true ですが、IsGenericTypeDefinition プロパティは false です。

注釈

このオーバーロードは、マネージド コンパイラのデザイナー向けに提供されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1