TypeBuilder.DefineDefaultConstructor(MethodAttributes) Metoda

Definice

Definuje konstruktor bez parametrů. Zde definovaný konstruktor jednoduše zavolá konstruktor bez parametrů nadřazeného objektu.

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

Parametry

attributes
MethodAttributes

Objekt MethodAttributes představující atributy, které se mají použít u konstruktoru.

Návraty

Vrátí konstruktor.

Atributy

Výjimky

Nadřazený typ (základní typ) nemá konstruktor bez parametrů.

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

nebo

U aktuálního dynamického typu IsGenericType je truevlastnost , ale vlastnost IsGenericTypeDefinition je false.

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

Vzhledem k tomu, že konstruktor bez parametrů je automaticky definován, je nutné volat tuto metodu pouze v následujících situacích:

  • Definovali jste jiný konstruktor a chcete také konstruktor bez parametrů, který jednoduše volá konstruktor základní třídy.

  • Chcete nastavit atributy konstruktoru bez parametrů na něco jiného než PrivateScope, Public, HideBySig, , SpecialNamea RTSpecialName.

Platí pro