TypeBuilder.DefineDefaultConstructor(MethodAttributes) Metod

Definition

Definierar den parameterlösa konstruktorn. Konstruktorn som definieras här anropar helt enkelt den överordnade konstruktorns parameterlösa konstruktor.

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

Parametrar

attributes
MethodAttributes

Ett MethodAttributes objekt som representerar de attribut som ska tillämpas på konstruktorn.

Returer

Returnerar konstruktorn.

Attribut

Undantag

Den överordnade typen (bastyp) har ingen parameterlös konstruktor.

Typen skapades tidigare med .CreateType()

-eller-

För den aktuella dynamiska typen är IsGenericTypeegenskapen true , men egenskapen IsGenericTypeDefinition är false.

Exempel

Följande kodexempel visar användningen av DefineConstructor för att ange en konstruktors specifika signatur och attribut på en dynamisk typ och returnera en motsvarande ConstructorBuilder för MSIL-populationen.

// 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)

Kommentarer

Eftersom den parameterlösa konstruktorn definieras automatiskt är det nödvändigt att anropa den här metoden endast i följande situationer:

  • Du har definierat en annan konstruktor och du vill också ha en parameterlös konstruktor som helt enkelt anropar basklasskonstruktorn.

  • Du vill ange attributen för den parameterlösa konstruktorn till något annat än PrivateScope, Public, HideBySig, SpecialNameoch RTSpecialName.

Gäller för