TypeBuilder.DefineConstructor Método

Definición

Agrega un constructor al tipo dinámico.

Sobrecargas

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Agrega un nuevo constructor al tipo, con los atributos y 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[])

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

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

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

Parámetros

attributes
MethodAttributes

Atributos del constructor.

callingConvention
CallingConventions

Convención de llamada del constructor.

parameterTypes
Type[]

Tipos de parámetro del constructor.

Devoluciones

El constructor definido.

Atributos

Excepciones

El tipo se creó previamente 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.

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

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 ha definido:

  • 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 por completo, debe usar el TypeBuilder.DefineConstructor método para crear uno y proporcionar su propia implementación.

Se aplica a

.NET 9 e outras versións
Produto Versións
.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[][])

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

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

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

Parámetros

attributes
MethodAttributes

Atributos del constructor.

callingConvention
CallingConventions

Convención de llamada del constructor.

parameterTypes
Type[]

Tipos de parámetro del constructor.

requiredCustomModifiers
Type[][]

Matriz de matrices de tipos. Cada matriz de tipos representa los modificadores personalizados obligatorios para el parámetro correspondiente, como IsConst. Si un parámetro concreto 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 concreto 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

El constructor definido.

Atributos

Excepciones

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

El tipo se creó previamente mediante CreateType().

o bien

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

Comentarios

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

Nota

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

Se aplica a

.NET 9 e outras versións
Produto Versións
.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