TypeBuilder.DefineDefaultConstructor(MethodAttributes) Metoda

Definice

Definuje konstruktor bez parametrů. Konstruktor, který je zde definován, jednoduše zavolá konstruktor bez parametrů nadřazené třídy.

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

MethodAttributesObjekt představující atributy, které mají být aplikovány na konstruktor.

Návraty

ConstructorBuilder

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- Pro aktuální dynamický typ IsGenericType je vlastnost true , ale IsGenericTypeDefinition vlastnost je false .

Příklady

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

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 také chcete konstruktor bez parametrů, který jednoduše volá konstruktor základní třídy.

  • Chcete nastavit atributy v konstruktoru bez parametrů na jinou hodnotu než PrivateScope , Public ,, HideBySig SpecialName a RTSpecialName .

Platí pro