Freigeben über


TypeBuilder.DefineDefaultConstructor-Methode

Definiert den Standardkonstruktor. Der hier definierte Konstruktor ruft lediglich den Standardkonstruktor des übergeordneten Elements auf.

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Function DefineDefaultConstructor ( _
    attributes As MethodAttributes _
) As ConstructorBuilder
'Usage
Dim instance As TypeBuilder
Dim attributes As MethodAttributes
Dim returnValue As ConstructorBuilder

returnValue = instance.DefineDefaultConstructor(attributes)
[ComVisibleAttribute(true)] 
public ConstructorBuilder DefineDefaultConstructor (
    MethodAttributes attributes
)
[ComVisibleAttribute(true)] 
public:
ConstructorBuilder^ DefineDefaultConstructor (
    MethodAttributes attributes
)
/** @attribute ComVisibleAttribute(true) */ 
public ConstructorBuilder DefineDefaultConstructor (
    MethodAttributes attributes
)
ComVisibleAttribute(true) 
public function DefineDefaultConstructor (
    attributes : MethodAttributes
) : ConstructorBuilder

Parameter

  • attributes
    Ein MethodAttributes-Objekt, das die auf den Konstruktor anzuwendenden Attribute darstellt.

Rückgabewert

Gibt den Konstruktor zurück.

Ausnahmen

Ausnahmetyp Bedingung

NotSupportedException

Der übergeordnete Typ (Basistyp) verfügt über keinen Standardkonstruktor.

InvalidOperationException

Der Typ wurde bereits mit CreateType erstellt.

- oder -

Für den aktuelle dynamischen Typ ist die IsGenericType-Eigenschaft true, die IsGenericTypeDefinition-Eigenschaft ist jedoch false.

Hinweise

Da der Standardkonstruktor automatisch definiert wird, müssen Sie diese Methode nur aufrufen, wenn die Attribute im Standardkonstruktor auf andere Werte als PrivateScope, Public, HideBySig, SpecialName und RTSpecialName festgelegt werden sollen.

Beispiel

Im folgenden Codebeispiel wird die Verwendung von DefineConstructor zum Festlegen der bestimmten Signatur für einen Konstruktor und von Attributen für einen dynamischen Typ sowie zum Zurückgeben eines entsprechenden ConstructorBuilder zur IL-Auffüllung veranschaulicht.

' 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)
// 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.
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 );

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

TypeBuilder-Klasse
TypeBuilder-Member
System.Reflection.Emit-Namespace