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

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,而非陣列的陣列。

傳回

定義的建構函式。

屬性

例外狀況

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

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

-或-

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

備註

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

適用於

.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