TypeBuilder.DefineConstructor Método

Definição

Adiciona um novo construtor ao tipo dinâmico.Adds a new constructor to the dynamic type.

Sobrecargas

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Adiciona um novo construtor ao tipo, com a assinatura e os atributos determinados.Adds a new constructor to the type, with the given attributes and signature.

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

Adiciona um novo construtor ao tipo, com os atributos, a assinatura e os modificadores personalizados determinados.Adds a new constructor to the type, with the given attributes, signature, and custom modifiers.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Adiciona um novo construtor ao tipo, com a assinatura e os atributos determinados.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

Parâmetros

attributes
MethodAttributes

Os atributos do construtor.The attributes of the constructor.

callingConvention
CallingConventions

A convenção de chamada do construtor.The calling convention of the constructor.

parameterTypes
Type[]

Os tipos de parâmetro do construtor.The parameter types of the constructor.

Retornos

ConstructorBuilder

O construtor definido.The defined constructor.

Atributos

Exceções

O tipo foi criado anteriormente usando CreateType().The type was previously created using CreateType().

Exemplos

O exemplo de código a seguir demonstra o uso de DefineConstructor para definir a assinatura e os atributos específicos de um construtor em um tipo dinâmico e retornar um correspondente ConstructorBuilder para população 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)

Comentários

Se você não definir um construtor para o tipo dinâmico, um construtor sem parâmetros será fornecido automaticamente e chamará o construtor sem parâmetros da classe base.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.

Se você definir um construtor para seu tipo dinâmico, um construtor sem parâmetros não será fornecido.If you define a constructor for your dynamic type, a parameterless constructor is not provided. Você tem as seguintes opções para fornecer um construtor sem parâmetros, além do construtor que você definiu:You have the following options for providing a parameterless constructor in addition to the constructor you defined:

  • Se você quiser um construtor sem parâmetros que simplesmente chame o construtor sem parâmetros da classe base, poderá usar o DefineDefaultConstructor método para criar um (e, opcionalmente, restringir o acesso a ele).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). Não forneça uma implementação para este construtor sem parâmetros.Do not provide an implementation for this parameterless constructor. Se você fizer isso, uma exceção será lançada quando você tentar usar o construtor.If you do, an exception is thrown when you try to use the constructor. Nenhuma exceção é lançada quando o CreateType método é chamado.No exception is thrown when the CreateType method is called.

  • Se você quiser um construtor sem parâmetros que faça algo mais do que simplesmente chamar o construtor sem parâmetros da classe base, ou que chame outro construtor da classe base ou que faça outra coisa inteiramente, você deverá usar o TypeBuilder.DefineConstructor método para criar um e fornecer sua própria implementação.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.

Aplica-se a

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

Adiciona um novo construtor ao tipo, com os atributos, a assinatura e os modificadores personalizados determinados.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

Parâmetros

attributes
MethodAttributes

Os atributos do construtor.The attributes of the constructor.

callingConvention
CallingConventions

A convenção de chamada do construtor.The calling convention of the constructor.

parameterTypes
Type[]

Os tipos de parâmetro do construtor.The parameter types of the constructor.

requiredCustomModifiers
Type[][]

Uma matriz de matrizes de tipos.An array of arrays of types. Cada matriz de tipos representa os modificadores personalizados obrigatórios para o parâmetro correspondente, por exemplo IsConst.Each array of types represents the required custom modifiers for the corresponding parameter, such as IsConst. Se um determinado parâmetro tiver não modificadores personalizados obrigatórios, especifique null em vez de uma matriz de tipos.If a particular parameter has no required custom modifiers, specify null instead of an array of types. Se nenhum dos parâmetros tiver modificadores personalizados obrigatórios, especifique null em vez de uma matriz de matrizes.If none of the parameters have required custom modifiers, specify null instead of an array of arrays.

optionalCustomModifiers
Type[][]

Uma matriz de matrizes de tipos.An array of arrays of types. Cada matriz de tipos representa os modificadores personalizados opcionais para o parâmetro correspondente, por exemplo IsConst.Each array of types represents the optional custom modifiers for the corresponding parameter, such as IsConst. Se um determinado parâmetro tiver não modificadores personalizados opcionais, especifique null em vez de uma matriz de tipos.If a particular parameter has no optional custom modifiers, specify null instead of an array of types. Se nenhum dos parâmetros tiver modificadores personalizados opcionais, especifique null em vez de uma matriz de matrizes.If none of the parameters have optional custom modifiers, specify null instead of an array of arrays.

Retornos

ConstructorBuilder

O construtor definido.The defined constructor.

Atributos

Exceções

O tamanho de requiredCustomModifiers ou optionalCustomModifiers não é igual ao tamanho de parameterTypes.The size of requiredCustomModifiers or optionalCustomModifiers does not equal the size of parameterTypes.

O tipo foi criado anteriormente usando CreateType().The type was previously created using CreateType().

- ou --or- Para o tipo dinâmico atual, a propriedade IsGenericType é true, mas a propriedade IsGenericTypeDefinition é false.For the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false.

Comentários

Essa sobrecarga é fornecida para designers de compiladores gerenciados.This overload is provided for designers of managed compilers.

Observação

Para obter mais informações sobre modificadores personalizados, consulte padrões ECMA C# e Common Language Infrastructure e a CLI (Standard ECMA-335-Common Language Infrastructure).For more information on custom modifiers, see ECMA C# and Common Language Infrastructure Standards and Standard ECMA-335 - Common Language Infrastructure (CLI).

Aplica-se a