TypeBuilder.DefineConstructor TypeBuilder.DefineConstructor TypeBuilder.DefineConstructor TypeBuilder.DefineConstructor Method

定义

将新的构造函数添加到动态类型。Adds a new constructor to the dynamic type.

重载

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

用给定的属性和签名,向类型中添加新的构造函数。Adds a new constructor to the type, with the given attributes and signature.

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) 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[]) DefineConstructor(MethodAttributes, CallingConventions, Type[]) DefineConstructor(MethodAttributes, CallingConventions, Type[]) 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);
[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
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type()) As ConstructorBuilder

参数

attributes
MethodAttributes MethodAttributes MethodAttributes MethodAttributes

构造函数的属性。The attributes of the constructor.

callingConvention
CallingConventions CallingConventions CallingConventions CallingConventions

构造函数的调用约定。The calling convention of the constructor.

parameterTypes
Type[]

构造函数的参数类型。The parameter types of the constructor.

返回

定义构造函数。The defined constructor.

异常

该类型是以前使用 CreateType() 创建的。The type was previously created using CreateType().

示例

下面的代码示例演示DefineConstructor如何使用在动态类型上设置构造函数的特定签名和特性,并为 MSIL 填充返回相应ConstructorBuilder的。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. 调用CreateType方法时不会引发异常。No 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[][]) DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) 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);
[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
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type(), requiredCustomModifiers As Type()(), optionalCustomModifiers As Type()()) As ConstructorBuilder

参数

attributes
MethodAttributes MethodAttributes MethodAttributes MethodAttributes

构造函数的属性。The attributes of the constructor.

callingConvention
CallingConventions CallingConventions CallingConventions 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.

返回

定义构造函数。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.

注解

此重载是为托管编译器的设计器提供的。This overload is provided for designers of managed compilers.

备注

有关自定义修饰符的详细信息,请参阅 ECMA Partition II 元数据文档。For more information on custom modifiers, see the ECMA Partition II Metadata documentation. 可联机获取该文档;请参阅 MSDN 上的 ECMA C# 和公共语言基础结构标准和 Ecma International 网站上的标准 ECMA-335 - 公共语言基础结构 (CLI)The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

适用于