Share via


TypeBuilder.DefineConstructor Yöntem

Tanım

Dinamik türe yeni bir oluşturucu ekler.

Aşırı Yüklemeler

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Belirtilen öznitelikler ve imza ile türüne yeni bir oluşturucu ekler.

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

Belirtilen öznitelikler, imza ve özel değiştiricilerle türe yeni bir oluşturucu ekler.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Kaynak:
TypeBuilder.cs
Kaynak:
TypeBuilder.cs
Kaynak:
TypeBuilder.cs

Belirtilen öznitelikler ve imza ile türüne yeni bir oluşturucu ekler.

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type()) As ConstructorBuilder

Parametreler

attributes
MethodAttributes

Oluşturucunun öznitelikleri.

callingConvention
CallingConventions

Oluşturucunun çağırma kuralı.

parameterTypes
Type[]

Oluşturucunun parametre türleri.

Döndürülenler

Tanımlanan oluşturucu.

Öznitelikler

Özel durumlar

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

Örnekler

Aşağıdaki kod örneği, bir oluşturucunun özel 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 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

Dinamik türünüz için bir oluşturucu tanımlamazsanız, otomatik olarak parametresiz bir oluşturucu sağlanır ve temel sınıfın parametresiz oluşturucusunu çağırır.

Dinamik türünüz için bir oluşturucu tanımlarsanız, parametresiz bir oluşturucu sağlanmaz. Tanımladığınız oluşturucuya ek olarak parametresiz bir oluşturucu sağlamak için aşağıdaki seçeneklere sahipsiniz:

  • Yalnızca temel sınıfın parametresiz oluşturucuyu çağıran parametresiz bir oluşturucu istiyorsanız, yöntemini kullanarak DefineDefaultConstructor bir tane oluşturabilir (ve isteğe bağlı olarak buna erişimi kısıtlayabilirsiniz). Bu parametresiz oluşturucu için bir uygulama sağlamayın. Bunu yaparsanız, oluşturucuyu kullanmaya çalıştığınızda bir özel durum oluşturulur. Yöntemi çağrıldığında CreateType hiçbir özel durum oluşturulur.

  • Yalnızca temel sınıfın parametresiz oluşturucusunun çağrılmasından daha fazlasını yapan veya temel sınıfın başka bir oluşturucusunu çağıran veya tamamen başka bir şey yapan parametresiz bir oluşturucu istiyorsanız, bir tane oluşturmak ve kendi uygulamanızı sağlamak için yöntemini kullanmanız TypeBuilder.DefineConstructor gerekir.

Şunlara uygulanır

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

Kaynak:
TypeBuilder.cs
Kaynak:
TypeBuilder.cs
Kaynak:
TypeBuilder.cs

Belirtilen öznitelikler, imza ve özel değiştiricilerle türe yeni bir oluşturucu ekler.

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ requiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ optionalCustomModifiers);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type(), requiredCustomModifiers As Type()(), optionalCustomModifiers As Type()()) As ConstructorBuilder

Parametreler

attributes
MethodAttributes

Oluşturucunun öznitelikleri.

callingConvention
CallingConventions

Oluşturucunun çağırma kuralı.

parameterTypes
Type[]

Oluşturucunun parametre türleri.

requiredCustomModifiers
Type[][]

Tür dizileri dizisi. Her tür dizisi, gibi IsConstilgili parametre için gerekli özel değiştiricileri temsil eder. Belirli bir parametrenin gerekli özel değiştiricileri yoksa, bir tür dizisi yerine belirtin null . Parametrelerin hiçbirinin özel değiştiricileri gerekli değilse, dizi dizisi yerine belirtin null .

optionalCustomModifiers
Type[][]

Tür dizileri dizisi. Her tür dizisi, IsConstgibi ilgili parametre için isteğe bağlı özel değiştiricileri temsil eder. Belirli bir parametrenin isteğe bağlı özel değiştiricisi yoksa, tür dizisi yerine belirtin null . Parametrelerin hiçbirinin isteğe bağlı özel değiştiricileri yoksa, dizi dizisi yerine belirtin null .

Döndürülenler

Tanımlanan oluşturucu.

Öznitelikler

Özel durumlar

veya optionalCustomModifiers boyuturequiredCustomModifiers, boyutuna parameterTypeseşit değildir.

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

-veya-

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

Açıklamalar

Bu aşırı yükleme, yönetilen derleyici tasarımcıları için sağlanır.

Not

Özel değiştiriciler hakkında daha fazla bilgi için bkz. ECMA C# ve Ortak Dil Altyapısı Standartları ve Standart ECMA-335 - Ortak Dil Altyapısı (CLI).

Şunlara uygulanır