TypeBuilder.DefineDefaultConstructor(MethodAttributes) Metodo

Definizione

Definisce il costruttore senza parametri. Il costruttore definito qui chiamerà semplicemente il costruttore senza parametri dell'elemento padre.

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

Parametri

attributes
MethodAttributes

Oggetto MethodAttributes che rappresenta gli attributi da applicare al costruttore.

Restituisce

ConstructorBuilder

Restituisce il costruttore.

Attributi

Eccezioni

Il tipo padre (tipo di base) non ha un costruttore senza parametri.

Il tipo è stato creato in precedenza usando CreateType().

-oppure- Per il tipo dinamico corrente, la proprietà IsGenericType è true ma la proprietà IsGenericTypeDefinition è false.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di per impostare la firma e gli attributi specifici di un costruttore su un tipo dinamico e restituire un oggetto corrispondente per DefineConstructor ConstructorBuilder il popolamento 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)

Commenti

Poiché il costruttore senza parametri viene definito automaticamente, è necessario chiamare questo metodo solo nelle situazioni seguenti:

  • È stato definito un altro costruttore e si vuole anche un costruttore senza parametri che chiami semplicemente il costruttore della classe base.

  • Si desidera impostare gli attributi nel costruttore senza parametri su un valore diverso da PrivateScope , , , e Public HideBySig SpecialName RTSpecialName .

Si applica a