TypeBuilder.DefineDefaultConstructor(MethodAttributes) Metoda

Definicja

Definiuje konstruktor bez parametrów. Konstruktor zdefiniowany w tym miejscu po prostu wywoła konstruktor bez parametrów obiektu nadrzędnego.

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

MethodAttributes Obiekt reprezentujący atrybuty, które mają zostać zastosowane do konstruktora.

Zwraca

Zwraca konstruktor.

Atrybuty

Wyjątki

Typ nadrzędny (typ podstawowy) nie ma konstruktora bez parametrów.

Typ został wcześniej utworzony przy użyciu polecenia CreateType().

-lub-

W przypadku bieżącego typu IsGenericType dynamicznego właściwość to true, ale IsGenericTypeDefinition właściwość to false.

Przykłady

W poniższym przykładzie kodu pokazano użycie metody DefineConstructor ustawiania określonego podpisu i atrybutów konstruktora dla typu dynamicznego i zwracania wartości odpowiadającej ConstructorBuilder populacji 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)

Uwagi

Ponieważ konstruktor bez parametrów jest definiowany automatycznie, należy wywołać tę metodę tylko w następujących sytuacjach:

  • Zdefiniowano inny konstruktor i chcesz również konstruktora bez parametrów, który po prostu wywołuje konstruktor klasy bazowej.

  • Chcesz ustawić atrybuty konstruktora bez parametrów na wartość inną niż PrivateScope, , HideBySigPublic, , SpecialNamei RTSpecialName.

Dotyczy