TypeBuilder.DefineConstructor Metoda

Definice

Přidá nový konstruktor k dynamickému typu.

Přetížení

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Přidá k typu nový konstruktor s danými atributy a podpisem.

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

Přidá k typu nový konstruktor s danými atributy, podpisem a vlastními modifikátory.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Zdroj:
TypeBuilder.cs
Zdroj:
TypeBuilder.cs
Zdroj:
TypeBuilder.cs

Přidá k typu nový konstruktor s danými atributy a podpisem.

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

Parametry

attributes
MethodAttributes

Atributy konstruktoru.

callingConvention
CallingConventions

Konvence volání konstruktoru.

parameterTypes
Type[]

Typy parametrů konstruktoru.

Návraty

Definovaný konstruktor.

Atributy

Výjimky

Typ byl dříve vytvořen pomocí příkazu CreateType().

Příklady

Následující ukázka kódu demonstruje použití DefineConstructor k nastavení konkrétního podpisu a atributů konstruktoru na dynamickém typu a vrácení odpovídající ConstructorBuilder pro soubor 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)

Poznámky

Pokud nedefinujete konstruktor pro dynamický typ, je konstruktor bez parametrů poskytován automaticky a volá konstruktor bez parametrů základní třídy.

Pokud definujete konstruktor pro dynamický typ, není zadán konstruktor bez parametrů. Kromě konstruktoru, který jste definovali, máte k dispozici následující možnosti pro poskytnutí konstruktoru bez parametrů:

  • Pokud chcete konstruktor bez parametrů, který jednoduše volá konstruktor bez parametrů základní třídy, můžete ho DefineDefaultConstructor vytvořit pomocí metody (a volitelně k němu omezit přístup). Neposkytujte implementaci pro tento konstruktor bez parametrů. Pokud ano, při pokusu o použití konstruktoru dojde k výjimce. Při zavolání CreateType metody není vyvolána žádná výjimka.

  • Pokud chcete bezparametrový konstruktor, který dělá něco víc než jen volání bezparametrového konstruktoru základní třídy, nebo který volá jiný konstruktor základní třídy, nebo který dělá něco úplně jiného, musíte použít metodu TypeBuilder.DefineConstructor k jeho vytvoření a poskytnout vlastní implementaci.

Platí pro

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

Zdroj:
TypeBuilder.cs
Zdroj:
TypeBuilder.cs
Zdroj:
TypeBuilder.cs

Přidá k typu nový konstruktor s danými atributy, podpisem a vlastními modifikátory.

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

Parametry

attributes
MethodAttributes

Atributy konstruktoru.

callingConvention
CallingConventions

Konvence volání konstruktoru.

parameterTypes
Type[]

Typy parametrů konstruktoru.

requiredCustomModifiers
Type[][]

Pole polí typů. Každé pole typů představuje požadované vlastní modifikátory pro odpovídající parametr, například IsConst. Pokud určitý parametr nemá žádné požadované vlastní modifikátory, zadejte null místo pole typů. Pokud žádný z parametrů nevyžaduje vlastní modifikátory, zadejte null místo pole pole.

optionalCustomModifiers
Type[][]

Pole polí typů. Každé pole typů představuje volitelné vlastní modifikátory pro odpovídající parametr, například IsConst. Pokud určitý parametr nemá žádné volitelné vlastní modifikátory, zadejte null místo pole typů. Pokud žádný z parametrů nemá volitelné vlastní modifikátory, zadejte null místo pole pole.

Návraty

Definovaný konstruktor.

Atributy

Výjimky

Velikost nebo requiredCustomModifiersoptionalCustomModifiers se nerovná velikosti .parameterTypes

Typ byl dříve vytvořen pomocí příkazu CreateType().

-nebo-

Pro aktuální dynamický typ IsGenericType je truevlastnost , ale IsGenericTypeDefinition vlastnost je false.

Poznámky

Toto přetížení je k dispozici pro návrháře spravovaných kompilátorů.

Platí pro