TypeBuilder.DefineConstructor Méthode

Définition

Ajoute un nouveau constructeur au type dynamique.

Surcharges

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Ajoute un nouveau constructeur au type avec les attributs donnés et la signature.

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

Ajoute un nouveau constructeur au type, avec les attributs, la signature et les modificateurs personnalisés donnés.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

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

Ajoute un nouveau constructeur au type avec les attributs donnés et la 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

Paramètres

attributes
MethodAttributes

Attributs du constructeur.

callingConvention
CallingConventions

Convention d’appel du constructeur.

parameterTypes
Type[]

Types de paramètre du constructeur.

Retours

Constructeur défini.

Attributs

Exceptions

Le type a déjà été créé en utilisant CreateType().

Exemples

L’exemple de code suivant illustre l’utilisation de DefineConstructor pour définir la signature et les attributs particuliers d’un constructeur sur un type dynamique et retourner un correspondant ConstructorBuilder pour la population 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)

Remarques

Si vous ne définissez pas de constructeur pour votre type dynamique, un constructeur sans paramètre est fourni automatiquement et il appelle le constructeur sans paramètre de la classe de base.

Si vous définissez un constructeur pour votre type dynamique, aucun constructeur sans paramètre n’est fourni. Vous disposez des options suivantes pour fournir un constructeur sans paramètre en plus du constructeur que vous avez défini :

  • Si vous souhaitez un constructeur sans paramètre qui appelle simplement le constructeur sans paramètre de la classe de base, vous pouvez utiliser la méthode pour en DefineDefaultConstructor créer un (et éventuellement restreindre l’accès à celle-ci). Ne fournissez pas d’implémentation pour ce constructeur sans paramètre. Dans ce cas, une exception est levée lorsque vous essayez d’utiliser le constructeur. Aucune exception n’est levée lorsque la CreateType méthode est appelée.

  • Si vous souhaitez un constructeur sans paramètre qui fait autre chose que simplement appeler le constructeur sans paramètre de la classe de base, ou qui appelle un autre constructeur de la classe de base, ou qui fait quelque chose d’autre entièrement, vous devez utiliser la TypeBuilder.DefineConstructor méthode pour en créer un et fournir votre propre implémentation.

S’applique à

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

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

Ajoute un nouveau constructeur au type, avec les attributs, la signature et les modificateurs personnalisés donnés.

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

Paramètres

attributes
MethodAttributes

Attributs du constructeur.

callingConvention
CallingConventions

Convention d’appel du constructeur.

parameterTypes
Type[]

Types de paramètre du constructeur.

requiredCustomModifiers
Type[][]

Tableau de tableaux de types. Chaque tableau de types représente les modificateurs personnalisés obligatoires pour le paramètre correspondant, comme IsConst. Si un paramètre particulier ne possède pas de modificateurs personnalisés obligatoires, spécifiez null plutôt qu’un tableau de types. Si aucun paramètre ne possède de modificateur personnalisé requis, spécifiez null plutôt qu’un tableau de tableaux.

optionalCustomModifiers
Type[][]

Tableau de tableaux de types. Chaque tableau de types représente les modificateurs personnalisés facultatifs pour le paramètre correspondant, comme IsConst. Si un paramètre particulier ne possède pas de modificateurs personnalisés facultatifs, spécifiez null plutôt qu’un tableau de types. Si aucun paramètre ne possède de modificateur personnalisé facultatif, spécifiez null plutôt qu’un tableau de tableaux.

Retours

Constructeur défini.

Attributs

Exceptions

La taille de requiredCustomModifiers ou optionalCustomModifiers n’est pas égale à la taille de parameterTypes.

Le type a déjà été créé en utilisant CreateType().

- ou -

Pour le type dynamique actuel, la propriété IsGenericType a la valeur true, mais la propriété IsGenericTypeDefinition a la valeur false.

Remarques

Cette surcharge est fournie aux concepteurs de compilateurs managés.

Notes

Pour plus d’informations sur les modificateurs personnalisés, consultez C# ECMA et Common Language Infrastructure Standards et Standard ECMA-335 - Common Language Infrastructure (CLI).

S’applique à