TypeBuilder.DefineConstructor Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Új konstruktort ad hozzá a dinamikus típushoz.
Túlterhelések
| Name | Description |
|---|---|
| DefineConstructor(MethodAttributes, CallingConventions, Type[]) |
Új konstruktort ad hozzá a típushoz a megadott attribútumokkal és aláírással. |
| DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) |
Új konstruktort ad hozzá a típushoz a megadott attribútumokkal, aláírással és egyéni módosítókkal. |
DefineConstructor(MethodAttributes, CallingConventions, Type[])
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
Új konstruktort ad hozzá a típushoz a megadott attribútumokkal és aláírással.
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éterek
- attributes
- MethodAttributes
A konstruktor attribútumai.
- callingConvention
- CallingConventions
A konstruktor hívó konvenciója.
- parameterTypes
- Type[]
A konstruktor paramétertípusai.
Válaszok
A definiált konstruktor.
- Attribútumok
Kivételek
A típus korábban a következővel lett létrehozva CreateType(): .
Példák
Az alábbi kódminta bemutatja, hogy egy DefineConstructor konstruktor adott aláírását és attribútumait dinamikus típusra kell beállítani, és visszaadni az MSIL-sokaságnak megfelelőt ConstructorBuilder .
// 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)
Megjegyzések
Ha nem definiál konstruktort a dinamikus típushoz, a rendszer automatikusan megadja a paraméter nélküli konstruktort, és meghívja az alaposztály paraméter nélküli konstruktorát.
Ha konstruktort határoz meg a dinamikus típushoz, a rendszer nem biztosít paraméter nélküli konstruktort. A definiált konstruktoron kívül az alábbi lehetőségek állnak rendelkezésre a paraméter nélküli konstruktor megadására:
Ha olyan paraméter nélküli konstruktort szeretne, amely egyszerűen meghívja az alaposztály paraméter nélküli konstruktorát, a metódussal DefineDefaultConstructor létrehozhat egyet (és opcionálisan korlátozhatja a hozzáférését). Ne adjon implementációt ehhez a paraméter nélküli konstruktorhoz. Ha így tesz, a rendszer kivételt jelez a konstruktor használatakor. A metódus meghívásakor CreateType a rendszer nem ad kivételt.
Ha olyan paraméter nélküli konstruktort szeretne, amely nem csupán az alaposztály paraméter nélküli konstruktorát hívja meg, vagy az alaposztály egy másik konstruktorát hívja meg, vagy teljesen mást csinál, akkor a TypeBuilder.DefineConstructor metódussal létre kell hoznia egyet, és saját implementációt kell biztosítania.
A következőre érvényes:
DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
- Forrás:
- TypeBuilder.cs
Új konstruktort ad hozzá a típushoz a megadott attribútumokkal, aláírással és egyéni módosítókkal.
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éterek
- attributes
- MethodAttributes
A konstruktor attribútumai.
- callingConvention
- CallingConventions
A konstruktor hívó konvenciója.
- parameterTypes
- Type[]
A konstruktor paramétertípusai.
- requiredCustomModifiers
- Type[][]
Típustömbök tömbje. Minden típusok tömbje a megfelelő paraméterhez szükséges egyéni módosítókat jelöli, például IsConst. Ha egy adott paraméterhez nincs szükség egyéni módosítókra, adja meg null a típusok tömbje helyett. Ha egyik paraméter sem rendelkezik kötelező egyéni módosítókkal, tömbtömb helyett adja meg null .
- optionalCustomModifiers
- Type[][]
Típustömbök tömbje. Minden típustömb a megfelelő paraméter opcionális egyéni módosítóit jelöli, például IsConst. Ha egy adott paraméter nem rendelkezik választható egyéni módosítókkal, adja meg null a típusok tömbje helyett. Ha egyik paraméter sem rendelkezik választható egyéni módosítókkal, tömbök tömbje helyett adja meg null .
Válaszok
A definiált konstruktor.
- Attribútumok
Kivételek
A méret vagy requiredCustomModifiersoptionalCustomModifiers nem egyenlő a méretével parameterTypes.
A típus korábban a következővel lett létrehozva CreateType(): .
-vagy-
Az aktuális dinamikus típus esetében a IsGenericType tulajdonság az true, de a IsGenericTypeDefinition tulajdonság az false.
Megjegyzések
Ez a túlterhelés a felügyelt fordítók tervezői számára biztosított.
Note
További információ az egyéni módosítókról: ECMA C# és Common Language Infrastructure Standards and Standard ECMA-335 – Common Language Infrastructure (CLI).