次の方法で共有


TypeBuilder.DefineConstructor メソッド

属性とシグネチャを指定して、新しいコンストラクタをクラスに追加します。

Public Function DefineConstructor( _
   ByVal attributes As MethodAttributes, _   ByVal callingConvention As CallingConventions, _   ByVal parameterTypes() As Type _) As ConstructorBuilder
[C#]
public ConstructorBuilder DefineConstructor(MethodAttributesattributes,CallingConventionscallingConvention,Type[] parameterTypes);
[C++]
public: ConstructorBuilder* DefineConstructor(MethodAttributesattributes,CallingConventionscallingConvention,Type* parameterTypes[]);
[JScript]
public function DefineConstructor(
   attributes : MethodAttributes,callingConvention : CallingConventions,parameterTypes : Type[]) : ConstructorBuilder;

パラメータ

  • attributes
    コンストラクタの属性。
  • callingConvention
    コンストラクタの呼び出し規約。
  • parameterTypes
    コンストラクタのパラメータの型。

戻り値

定義されたコンストラクタ。

例外

例外の種類 条件
InvalidOperationException この型は、 CreateType を使用して既に作成されています。

使用例

[Visual Basic, C#, C++] 次のコード例は、 DefineConstructor を使用して、コンストラクタの固有のシグネチャと属性を動的な型に対して設定し、それに対応する MSIL に書き込みを行うための ConstructorBuilder を返す方法を示しています。

 
' 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)

[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);

[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);

[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 名前空間