TypeBuilder.DefineDefaultConstructor(MethodAttributes) Metódus

Definíció

Meghatározza a paraméter nélküli konstruktort. Az itt definiált konstruktor egyszerűen meghívja a szülő paraméter nélküli konstruktorát.

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineDefaultConstructor(System::Reflection::MethodAttributes attributes);
public System.Reflection.Emit.ConstructorBuilder DefineDefaultConstructor(System.Reflection.MethodAttributes attributes);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineDefaultConstructor(System.Reflection.MethodAttributes attributes);
member this.DefineDefaultConstructor : System.Reflection.MethodAttributes -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineDefaultConstructor : System.Reflection.MethodAttributes -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineDefaultConstructor (attributes As MethodAttributes) As ConstructorBuilder

Paraméterek

attributes
MethodAttributes

A MethodAttributes konstruktorra alkalmazni kívánt attribútumokat képviselő objektum.

Válaszok

A konstruktort adja vissza.

Attribútumok

Kivételek

A szülőtípus (alaptípus) nem rendelkezik paraméter nélküli konstruktorsal.

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.

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

Mivel a paraméter nélküli konstruktor automatikusan van definiálva, ezt a metódust csak a következő helyzetekben kell meghívni:

  • Definiált egy másik konstruktort, és olyan paraméter nélküli konstruktort is szeretne, amely egyszerűen meghívja az alaposztály-konstruktort.

  • A paraméter nélküli konstruktor attribútumait másra szeretné beállítani, mint PrivateScopea , Public, HideBySig, SpecialNameés RTSpecialName.

A következőre érvényes: