TypeBuilder.DefineDefaultConstructor(MethodAttributes) Metoda

Definice

Definuje konstruktor bez parametrů. Konstruktor definovaný zde bude jednoduše volat konstruktor nadřazeného objektu bez parametrů.

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é mají být použity 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í příkazu CreateType().

-nebo-

Pro aktuální dynamický typ IsGenericType je truevlastnost , ale IsGenericTypeDefinition vlastnost je false.

Příklady

Následující ukázka kódu demonstruje použití DefineConstructor k nastavení konkrétního podpisu a atributů konstruktoru na dynamickém typu a vrácení odpovídající ConstructorBuilder pro soubor 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 definován automaticky, 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, , PublicHideBySig, SpecialNamea RTSpecialName.

Platí pro