TypeBuilder.DefineConstructor Metod

Definition

Lägger till en ny konstruktor till den dynamiska typen.

Överlagringar

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

Lägger till en ny konstruktor till typen med de angivna attributen och signaturen.

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

Lägger till en ny konstruktor till typen med angivna attribut, signaturer och anpassade modifierare.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Lägger till en ny konstruktor till typen med de angivna attributen och signaturen.

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

Parametrar

attributes
MethodAttributes

Konstruktorns attribut.

callingConvention
CallingConventions

Konstruktorns anropskonvention.

parameterTypes
Type[]

Parametertyperna för konstruktorn.

Returer

Den definierade konstruktorn.

Attribut

Undantag

Typen skapades tidigare med .CreateType()

Exempel

Följande kodexempel visar användningen av DefineConstructor för att ange en konstruktors specifika signatur och attribut på en dynamisk typ och returnera en motsvarande ConstructorBuilder för MSIL-populationen.

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

Kommentarer

Om du inte definierar en konstruktor för din dynamiska typ tillhandahålls en parameterlös konstruktor automatiskt och anropar den parameterlösa konstruktorn för basklassen.

Om du definierar en konstruktor för din dynamiska typ tillhandahålls ingen parameterlös konstruktor. Du har följande alternativ för att tillhandahålla en parameterlös konstruktor utöver konstruktorn som du definierade:

  • Om du vill ha en parameterlös konstruktor som helt enkelt anropar den parameterlösa konstruktorn för basklassen DefineDefaultConstructor kan du använda metoden för att skapa en (och eventuellt begränsa åtkomsten till den). Ange ingen implementering för den här parameterlösa konstruktorn. Om du gör det utlöses ett undantag när du försöker använda konstruktorn. Inget undantag utlöses när CreateType metoden anropas.

  • Om du vill ha en parameterlös konstruktor som gör något mer än att bara anropa den parameterlösa konstruktorn för basklassen, eller som anropar en annan konstruktor i basklassen, eller som gör något helt annat, måste du använda TypeBuilder.DefineConstructor metoden för att skapa en och tillhandahålla din egen implementering.

Gäller för

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

Lägger till en ny konstruktor till typen med angivna attribut, signaturer och anpassade modifierare.

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

Parametrar

attributes
MethodAttributes

Konstruktorns attribut.

callingConvention
CallingConventions

Konstruktorns anropskonvention.

parameterTypes
Type[]

Parametertyperna för konstruktorn.

requiredCustomModifiers
Type[][]

En matris med matriser av typer. Varje matris med typer representerar nödvändiga anpassade modifierare för motsvarande parameter, till exempel IsConst. Om en viss parameter inte har några nödvändiga anpassade modifierare anger du null i stället för en matris med typer. Om ingen av parametrarna har nödvändiga anpassade modifierare anger du null i stället för en matris med matriser.

optionalCustomModifiers
Type[][]

En matris med matriser av typer. Varje matris med typer representerar valfria anpassade modifierare för motsvarande parameter, till exempel IsConst. Om en viss parameter inte har några valfria anpassade modifierare anger du null i stället för en matris med typer. Om ingen av parametrarna har valfria anpassade modifierare anger du null i stället för en matris med matriser.

Returer

Den definierade konstruktorn.

Attribut

Undantag

Storleken på requiredCustomModifiers eller optionalCustomModifiers är inte lika med storleken på parameterTypes.

Typen skapades tidigare med .CreateType()

-eller-

För den aktuella dynamiska typen är trueegenskapen IsGenericType , men egenskapen IsGenericTypeDefinition är false.

Kommentarer

Den här överbelastningen tillhandahålls för designers av hanterade kompilatorer.

Gäller för