TypeBuilder.DefineConstructor Método

Definición

Agrega un nuevo constructor al tipo dinámico.

Sobrecargas

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

Agrega un nuevo constructor al tipo, con los atributos y la firma especificados.

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

Agrega un nuevo constructor al tipo, con los atributos, la firma y los modificadores personalizados especificados.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Agrega un nuevo constructor al tipo, con los atributos y la firma especificados.

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

Parámetros

attributes
MethodAttributes

Atributos del constructor.

callingConvention
CallingConventions

Convención de llamada del constructor.

parameterTypes
Type[]

Los tipos de parámetro del constructor.

Devoluciones

Constructor definido.

Atributos

Excepciones

El tipo se creó anteriormente mediante CreateType().

Ejemplos

En el ejemplo de código siguiente se muestra el uso de para establecer la firma y los atributos concretos de DefineConstructor un constructor en un tipo dinámico y devolver un correspondiente ConstructorBuilder para el rellenado de 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)

Comentarios

Si no define un constructor para el tipo dinámico, se proporciona automáticamente un constructor sin parámetros y llama al constructor sin parámetros de la clase base.

Si define un constructor para el tipo dinámico, no se proporciona un constructor sin parámetros. Tiene las siguientes opciones para proporcionar un constructor sin parámetros además del constructor que definió:

  • Si desea un constructor sin parámetros que simplemente llame al constructor sin parámetros de la clase base, puede usar el DefineDefaultConstructor método para crear uno (y, opcionalmente, restringir el acceso a ella). No proporcione una implementación para este constructor sin parámetros. Si lo hace, se produce una excepción al intentar usar el constructor . No se produce ninguna excepción cuando se llama al CreateType método .

  • Si desea un constructor sin parámetros que haga algo más que simplemente llamar al constructor sin parámetros de la clase base, o que llame a otro constructor de la clase base, o que haga algo más completamente, debe usar el TypeBuilder.DefineConstructor método para crear uno y proporcionar su propia implementación.

Se aplica a

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

Agrega un nuevo constructor al tipo, con los atributos, la firma y los modificadores personalizados especificados.

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

Parámetros

attributes
MethodAttributes

Atributos del constructor.

callingConvention
CallingConventions

Convención de llamada del constructor.

parameterTypes
Type[]

Los tipos de parámetro del constructor.

requiredCustomModifiers
Type[][]

Matriz de matrices de tipos. Cada matriz de tipos representa los modificadores personalizados necesarios para el parámetro correspondiente, como IsConst. Si un parámetro determinado no tiene modificadores personalizados necesarios, especifique null en lugar de una matriz de tipos. Si ninguno de los parámetros tiene modificadores personalizados necesarios, especifique null en lugar de una matriz de matrices.

optionalCustomModifiers
Type[][]

Matriz de matrices de tipos. Cada matriz de tipos representa los modificadores personalizados opcionales para el parámetro correspondiente, como IsConst. Si un parámetro determinado no tiene modificadores personalizados opcionales, especifique null en lugar de una matriz de tipos. Si ninguno de los parámetros tiene modificadores personalizados opcionales, especifique null en lugar de una matriz de matrices.

Devoluciones

Constructor definido.

Atributos

Excepciones

El tamaño de requiredCustomModifiers o optionalCustomModifiers no es igual al tamaño de parameterTypes.

El tipo se creó anteriormente mediante CreateType().

O bien

Para el tipo dinámico actual, la IsGenericType propiedad es true, pero la IsGenericTypeDefinition propiedad es false.

Comentarios

Esta sobrecarga se proporciona para diseñadores de compiladores administrados.

Note

Para obtener más información sobre los modificadores personalizados, consulte ECMA C# y Common Language Infrastructure Standards and Standard ECMA-335 - Common Language Infrastructure (CLI).

Se aplica a