Share via


TypeBuilder.DefineDefaultConstructor(MethodAttributes) Yöntem

Tanım

Parametresiz oluşturucuyu tanımlar. Burada tanımlanan oluşturucu yalnızca üst öğesinin parametresiz oluşturucuyu çağırır.

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

Parametreler

attributes
MethodAttributes

MethodAttributes Oluşturucuya uygulanacak öznitelikleri temsil eden bir nesne.

Döndürülenler

Oluşturucuyu döndürür.

Öznitelikler

Özel durumlar

Üst türün (temel tür) parametresiz oluşturucu yok.

Türü daha önce kullanılarak CreateType()oluşturulmuştur.

-veya-

Geçerli dinamik türü için IsGenericType özelliği şeklindedir true, ancak IsGenericTypeDefinition özelliği şeklindedir false.

Örnekler

Aşağıdaki kod örneği, bir oluşturucunun belirli imzasını ve özniteliklerini dinamik bir türe ayarlamak ve MSIL popülasyonu için karşılık gelen ConstructorBuilder bir döndürmek için uygulamasının kullanımını DefineConstructor gösterir.

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

Açıklamalar

Parametresiz oluşturucu otomatik olarak tanımlandığından, bu yöntemi yalnızca aşağıdaki durumlarda çağırmak gerekir:

  • Başka bir oluşturucu tanımladığınız gibi, yalnızca temel sınıf oluşturucuyu çağıran parametresiz bir oluşturucu da istiyorsunuz.

  • Parametresiz oluşturucudaki öznitelikleri , , Public, SpecialNameHideBySigve RTSpecialNamedışında PrivateScopebir değere ayarlamak istiyorsunuz.

Şunlara uygulanır