TypeBuilder.DefineDefaultConstructor(MethodAttributes) Methode

Definitie

Definieert de parameterloze constructor. De constructor die hier is gedefinieerd, roept gewoon de parameterloze constructor van het bovenliggende element aan.

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

Parameters

attributes
MethodAttributes

Een MethodAttributes object dat de kenmerken weergeeft die moeten worden toegepast op de constructor.

Retouren

Retourneert de constructor.

Kenmerken

Uitzonderingen

Het bovenliggende type (basistype) heeft geen parameterloze constructor.

Het type is eerder gemaakt met behulp van CreateType().

– of –

Voor het huidige dynamische type is de IsGenericType eigenschap , maar de IsGenericTypeDefinition eigenschap is false.true

Voorbeelden

In het volgende codevoorbeeld wordt het gebruik gedemonstreerd van het instellen van DefineConstructor de specifieke handtekening en kenmerken van een constructor voor een dynamisch type en het retourneren van een corresponderende ConstructorBuilder MSIL-populatie.

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

Opmerkingen

Omdat de parameterloze constructor automatisch wordt gedefinieerd, is het noodzakelijk om deze methode alleen in de volgende situaties aan te roepen:

  • U hebt een andere constructor gedefinieerd en u wilt ook een parameterloze constructor die gewoon de basisklasseconstructor aanroept.

  • U wilt de kenmerken van de parameterloze constructor instellen op iets anders dan , , , en RTSpecialNameSpecialName. HideBySigPublicPrivateScope

Van toepassing op