TypeBuilder.DefineConstructor Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Přidá nový konstruktor do dynamického typu.
Přetížení
| Name | Description |
|---|---|
| DefineConstructor(MethodAttributes, CallingConventions, Type[]) |
Přidá do typu nový konstruktor s danými atributy a podpisem. |
| DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) |
Přidá do 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
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
Přidá do 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í CreateType().
Příklady
Následující ukázka kódu ukazuje použití DefineConstructor k nastavení konkrétního podpisu a atributů konstruktoru na dynamickém typu a vrácení odpovídající ConstructorBuilder pro základní soubor JAZYKA MSIL.
// 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 váš dynamický typ, poskytuje se konstruktor bez parametrů automaticky a volá konstruktor bez parametrů základní třídy.
Pokud definujete konstruktor pro váš dynamický typ, není zadán konstruktor bez parametrů. Kromě konstruktoru, který jste definovali, máte následující možnosti pro poskytování konstruktoru bez parametrů:
Pokud chcete konstruktor bez parametrů, který jednoduše volá konstruktor bez parametrů základní třídy, můžete použít metodu DefineDefaultConstructor k vytvoření (a volitelně omezit přístup k ní). Neposkytujte implementaci tohoto konstruktoru bez parametrů. Pokud ano, při pokusu o použití konstruktoru se vyvolá výjimka. Při CreateType zavolání metody není vyvolána žádná výjimka.
Pokud chcete konstruktor bez parametrů, který dělá něco víc než jednoduše volání konstruktoru bez parametrů základní třídy, nebo který volá jiný konstruktor základní třídy, nebo který dělá něco jiného zcela, musíte použít metodu TypeBuilder.DefineConstructor k vytvoření a poskytnout vlastní implementaci.
Platí pro
DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
Přidá do 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 polí.
- optionalCustomModifiers
- Type[][]
Pole polí typů. Každé pole typů představuje volitelné vlastní modifikátory pro odpovídající parametr, například IsConst. Pokud konkrétní 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 polí.
Návraty
Definovaný konstruktor.
- Atributy
Výjimky
Velikost requiredCustomModifiers nebo optionalCustomModifiers se nerovná velikosti parameterTypes.
Typ byl dříve vytvořen pomocí CreateType().
nebo
U aktuálního dynamického typu IsGenericType je truevlastnost , ale vlastnost IsGenericTypeDefinition je false.
Poznámky
Toto přetížení je poskytováno pro návrháře spravovaných kompilátorů.
Note
Další informace o vlastních modifikátorech najdete v tématu ECMA C# a standardECMA-335 – Common Language Infrastructure (CLI).