TypeBuilder.DefineConstructor 方法

定義

將新建構函式加入動態類型中。

多載

DefineConstructor(MethodAttributes, CallingConventions, Type[])

將新的建構函式加入此類型,並指定屬性和簽章。

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

將新的建構函式加入此類型,並指定屬性、簽章和自訂修飾詞。

DefineConstructor(MethodAttributes, CallingConventions, Type[])

來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs

將新的建構函式加入此類型,並指定屬性和簽章。

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);
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 在動態類型上設定建構函式的特定簽章和屬性,並傳回對應 ConstructorBuilder 至 MSIL 母體擴展的 。

// Define the constructor.
array<Type^>^ constructorArgs = {String::typeid};
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.
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[][])

來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs

將新的建構函式加入此類型,並指定屬性、簽章和自訂修飾詞。

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);
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)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
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,而非陣列的陣列。

傳回

定義的建構函式。

屬性

例外狀況

requiredCustomModifiersoptionalCustomModifiers 的大小不等於 parameterTypes 的大小。

先前使用 CreateType() 建立的類型。

-或-

目前動態類型的 IsGenericType 屬性為 true,但 IsGenericTypeDefinition 屬性為 false

備註

這個多載是提供給 Managed 編譯器的設計工具。

適用於