TypeBuilder.DefineConstructor 方法

定義

為動態型別新增一個建構子。

多載

名稱 Description
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 動態型別上設定建構子的特定簽名與屬性,並回傳對應 ConstructorBuilder 的 MSIL 族群。

// 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。 如果某個參數沒有必要的自訂修飾符,則改用 Display null 代替型別陣列。 如果這些參數都不需要自訂修飾符,請指定 null 為 代替陣列的陣列。

optionalCustomModifiers
Type[][]

一組類型的陣列。 每個類型陣列代表對應參數的可選自訂修飾符,例如 IsConst。 如果某個參數沒有可選的自訂修飾符,則用指定 null 代替型別陣列。 如果沒有任何參數有可選的自訂修飾符,則指定 null 為 ,而不是陣列的陣列。

傳回

定義的建構子。

屬性

例外狀況

optionalCustomModifiersrequiredCustomModifiers大小不等於 的parameterTypes大小。

該類型先前是使用 CreateType()

-或-

對於目前的動態型態, IsGenericType 屬性為 true,但性質 IsGenericTypeDefinitionfalse

備註

這種過載是為受管編譯器設計者提供的。

Note

欲了解更多自訂修飾符的資訊,請參閱 ECMA C# 與通用語言基礎設施標準,以及標準 ECMA-335 - 通用語言基礎架構(CLI)。

適用於