TypeBuilder.DefineConstructor 方法

定義

將新建構函式加入動態類型中。Adds a new constructor to the dynamic type.

多載

DefineConstructor(MethodAttributes, CallingConventions, Type[])

將新的建構函式加入此類型,並指定屬性和簽章。Adds a new constructor to the type, with the given attributes and signature.

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

將新的建構函式加入此類型,並指定屬性、簽章和自訂修飾詞。Adds a new constructor to the type, with the given attributes, signature, and custom modifiers.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

將新的建構函式加入此類型,並指定屬性和簽章。Adds a new constructor to the type, with the given attributes and signature.

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

建構函式的屬性。The attributes of the constructor.

callingConvention
CallingConventions

建構函式的呼叫慣例。The calling convention of the constructor.

parameterTypes
Type[]

建構函式的參數類型。The parameter types of the constructor.

傳回

ConstructorBuilder

定義的建構函式。The defined constructor.

屬性

例外狀況

先前使用 CreateType() 建立的類型。The type was previously created using CreateType().

範例

下列程式碼範例將示範如何使用 DefineConstructor 來設定動態類型的函式的特定簽章和屬性,並傳回對應 ConstructorBuilder 的 MSIL 擴展。The following code sample demonstrates the use of DefineConstructor to set a constructor's particular signature and attributes on a dynamic type and return a corresponding ConstructorBuilder for MSIL population.

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

備註

如果您未定義動態類型的函式,則會自動提供無參數的函式,並呼叫基類的無參數函式。If you do not define a constructor for your dynamic type, a parameterless constructor is provided automatically, and it calls the parameterless constructor of the base class.

如果您為動態類型定義了一個函式,則不會提供無參數的函式。If you define a constructor for your dynamic type, a parameterless constructor is not provided. 除了您定義的函式之外,您還可以使用下列選項來提供無參數的函式:You have the following options for providing a parameterless constructor in addition to the constructor you defined:

  • 如果您想要無參數的函式,只呼叫基類的無參數函式,您可以使用 DefineDefaultConstructor 方法建立一個 (,並選擇性地限制對它) 的存取。If you want a parameterless constructor that simply calls the parameterless constructor of the base class, you can use the DefineDefaultConstructor method to create one (and optionally restrict access to it). 請勿提供這個無參數的函式的執行。Do not provide an implementation for this parameterless constructor. 如果您這樣做,當您嘗試使用此函式時,就會擲回例外狀況。If you do, an exception is thrown when you try to use the constructor. 呼叫方法時,不會擲回例外狀況 CreateTypeNo exception is thrown when the CreateType method is called.

  • 如果您想要執行非參數的函式,而不是只呼叫基類的無參數函式,或是呼叫基類的另一個函式,或是完全執行其他動作,您必須使用 TypeBuilder.DefineConstructor 方法建立一個,並提供您自己的實作為。If you want a parameterless constructor that does something more than simply calling the parameterless constructor of the base class, or that calls another constructor of the base class, or that does something else entirely, you must use the TypeBuilder.DefineConstructor method to create one, and provide your own implementation.

適用於

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

將新的建構函式加入此類型,並指定屬性、簽章和自訂修飾詞。Adds a new constructor to the type, with the given attributes, signature, and custom modifiers.

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

建構函式的屬性。The attributes of the constructor.

callingConvention
CallingConventions

建構函式的呼叫慣例。The calling convention of the constructor.

parameterTypes
Type[]

建構函式的參數類型。The parameter types of the constructor.

requiredCustomModifiers
Type[][]

類型陣列的陣列。An array of arrays of types. 每個類型陣列都代表其對應參數必要的自訂修飾詞,例如 IsConstEach array of types represents the required custom modifiers for the corresponding parameter, such as IsConst. 如果特定的參數沒有必要的自訂修飾詞,則指定 null,而非類型陣列。If a particular parameter has no required custom modifiers, specify null instead of an array of types. 如果這些參數都沒有必要的自訂修飾詞,則指定 null,而非陣列的陣列。If none of the parameters have required custom modifiers, specify null instead of an array of arrays.

optionalCustomModifiers
Type[][]

類型陣列的陣列。An array of arrays of types. 每個類型陣列都代表其對應參數的選擇性自訂修飾詞,例如 IsConstEach array of types represents the optional custom modifiers for the corresponding parameter, such as IsConst. 如果特定的參數沒有選擇性的自訂修飾詞,則指定 null,而非類型陣列。If a particular parameter has no optional custom modifiers, specify null instead of an array of types. 如果這些參數都沒有選擇性的自訂修飾詞,則指定 null,而非陣列的陣列。If none of the parameters have optional custom modifiers, specify null instead of an array of arrays.

傳回

ConstructorBuilder

定義的建構函式。The defined constructor.

屬性

例外狀況

requiredCustomModifiersoptionalCustomModifiers 的大小不等於 parameterTypes 的大小。The size of requiredCustomModifiers or optionalCustomModifiers does not equal the size of parameterTypes.

先前使用 CreateType() 建立的類型。The type was previously created using CreateType().

-或--or- 目前動態類型的 IsGenericType 屬性為 true,但 IsGenericTypeDefinition 屬性為 falseFor the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false.

備註

針對 managed 編譯器的設計工具會提供此多載。This overload is provided for designers of managed compilers.

適用於