TypeBuilder.DefineConstructor 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将新的构造函数添加到动态类型。
重载
| 名称 | 说明 |
|---|---|
| DefineConstructor(MethodAttributes, CallingConventions, Type[]) |
使用给定的属性和签名向类型添加新构造函数。 |
| DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) |
使用给定的属性、签名和自定义修饰符向类型添加新构造函数。 |
DefineConstructor(MethodAttributes, CallingConventions, Type[])
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- 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.
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[][])
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- 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 而不是数组数组。
返回
定义的构造函数。
- 属性
例外
大小 requiredCustomModifiers 或 optionalCustomModifiers 大小不等于 parameterTypes。
注解
此重载是为托管编译器的设计器提供的。
注释
有关自定义修饰符的详细信息,请参阅 ECMA C# 和公共语言基础结构标准和标准 ECMA-335 - 公共语言基础结构 (CLI)。