TypeBuilder.DefineConstructor Metodo

Definizione

Aggiunge un nuovo costruttore al tipo dinamico.

Overload

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Aggiunge un nuovo costruttore al tipo, con la firma e gli attributi specificati.

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

Aggiunge un nuovo costruttore al tipo, con gli attributi specificati, la firma e i modificatori personalizzati.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

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

Aggiunge un nuovo costruttore al tipo, con la firma e gli attributi specificati.

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

Parametri

attributes
MethodAttributes

Attributi del costruttore.

callingConvention
CallingConventions

Convenzione di chiamata del costruttore.

parameterTypes
Type[]

Tipi di parametro del costruttore.

Restituisce

Costruttore definito.

Attributi

Eccezioni

Il tipo è stato creato in precedenza usando CreateType().

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di DefineConstructor per impostare la firma e gli attributi specifici di un costruttore su un tipo dinamico e restituire un oggetto corrispondente ConstructorBuilder per il popolamento MSIL.

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

Commenti

Se non si definisce un costruttore per il tipo dinamico, viene fornito automaticamente un costruttore senza parametri e chiama il costruttore senza parametri della classe base.

Se si definisce un costruttore per il tipo dinamico, non viene fornito un costruttore senza parametri. Sono disponibili le opzioni seguenti per fornire un costruttore senza parametri oltre al costruttore definito:

  • Se si desidera un costruttore senza parametri che chiama semplicemente il costruttore senza parametri della classe base, è possibile usare il DefineDefaultConstructor metodo per crearne uno (e, facoltativamente, limitare l'accesso a tale costruttore). Non fornire un'implementazione per questo costruttore senza parametri. In tal caso, viene generata un'eccezione quando si tenta di usare il costruttore . Non viene generata alcuna eccezione quando viene chiamato il CreateType metodo .

  • Se si desidera un costruttore senza parametri che esegua un'operazione più che semplicemente chiamare il costruttore senza parametri della classe di base o che chiami un altro costruttore della classe base o che esegue completamente un'operazione diversa, è necessario usare il TypeBuilder.DefineConstructor metodo per crearne uno e fornire la propria implementazione.

Si applica a

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

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

Aggiunge un nuovo costruttore al tipo, con gli attributi specificati, la firma e i modificatori personalizzati.

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

Parametri

attributes
MethodAttributes

Attributi del costruttore.

callingConvention
CallingConventions

Convenzione di chiamata del costruttore.

parameterTypes
Type[]

Tipi di parametro del costruttore.

requiredCustomModifiers
Type[][]

Matrice di matrici di tipi. Ogni matrice di tipi rappresenta i modificatori personalizzati obbligatori per il parametro corrispondente, come IsConst. Se un parametro particolare non ha modificatori personalizzati obbligatori, specificare null invece di una matrice di tipi. Se nessun parametro ha modificatori personalizzati obbligatori, specificare null invece di una matrice di matrici.

optionalCustomModifiers
Type[][]

Matrice di matrici di tipi. Ogni matrice di tipi rappresenta i modificatori personalizzati facoltativi per il parametro corrispondente, come IsConst. Se un parametro particolare non ha modificatori personalizzati facoltativi, specificare null invece di una matrice di tipi. Se nessun parametro ha modificatori personalizzati facoltativi, specificare null invece di una matrice di matrici.

Restituisce

Costruttore definito.

Attributi

Eccezioni

La dimensione di requiredCustomModifiers o optionalCustomModifiers non è uguale a quella di parameterTypes.

Il tipo è stato creato in precedenza usando CreateType().

-oppure-

Per il tipo dinamico corrente, la proprietà IsGenericType è true ma la proprietà IsGenericTypeDefinition è false.

Commenti

Questo overload viene fornito per le finestre di progettazione dei compilatori gestiti.

Nota

Per altre informazioni sui modificatori personalizzati, vedere ECMA C# e Common Language Infrastructure Standards e Standard ECMA-335 - Common Language Infrastructure (CLI).

Si applica a