TypeBuilder.DefineConstructor Metode

Definisi

Menambahkan konstruktor baru ke jenis dinamis.

Overload

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Menambahkan konstruktor baru ke jenis , dengan atribut dan tanda tangan yang diberikan.

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

Menambahkan konstruktor baru ke jenis , dengan atribut, tanda tangan, dan pengubah kustom yang diberikan.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Sumber:
TypeBuilder.cs
Sumber:
TypeBuilder.cs
Sumber:
TypeBuilder.cs

Menambahkan konstruktor baru ke jenis , dengan atribut dan tanda tangan yang diberikan.

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

Parameter

attributes
MethodAttributes

Atribut konstruktor.

callingConvention
CallingConventions

Konvensi panggilan konstruktor.

parameterTypes
Type[]

Jenis parameter konstruktor.

Mengembalikan

Konstruktor yang ditentukan.

Atribut

Pengecualian

Jenis sebelumnya dibuat menggunakan CreateType().

Contoh

Sampel kode berikut menunjukkan penggunaan DefineConstructor untuk mengatur tanda tangan dan atribut khusus konstruktor pada jenis dinamis dan mengembalikan yang sesuai ConstructorBuilder untuk populasi MSIL.

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

Keterangan

Jika Anda tidak menentukan konstruktor untuk jenis dinamis Anda, konstruktor tanpa parameter disediakan secara otomatis, dan memanggil konstruktor tanpa parameter dari kelas dasar.

Jika Anda menentukan konstruktor untuk jenis dinamis Anda, konstruktor tanpa parameter tidak disediakan. Anda memiliki opsi berikut untuk menyediakan konstruktor tanpa parameter selain konstruktor yang Anda tentukan:

  • Jika Anda menginginkan konstruktor tanpa parameter yang hanya memanggil konstruktor tanpa parameter dari kelas dasar, Anda dapat menggunakan DefineDefaultConstructor metode untuk membuatnya (dan secara opsional membatasi akses ke dalamnya). Jangan berikan implementasi untuk konstruktor tanpa parameter ini. Jika Anda melakukannya, pengecualian dilemparkan ketika Anda mencoba menggunakan konstruktor. Tidak ada pengecualian yang dilemparkan ketika CreateType metode dipanggil.

  • Jika Anda menginginkan konstruktor tanpa parameter yang melakukan sesuatu lebih dari sekadar memanggil konstruktor tanpa parameter dari kelas dasar, atau yang memanggil konstruktor lain dari kelas dasar, atau yang melakukan sesuatu yang lain sepenuhnya, Anda harus menggunakan TypeBuilder.DefineConstructor metode untuk membuatnya, dan menyediakan implementasi Anda sendiri.

Berlaku untuk

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

Sumber:
TypeBuilder.cs
Sumber:
TypeBuilder.cs
Sumber:
TypeBuilder.cs

Menambahkan konstruktor baru ke jenis , dengan atribut, tanda tangan, dan pengubah kustom yang diberikan.

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

Parameter

attributes
MethodAttributes

Atribut konstruktor.

callingConvention
CallingConventions

Konvensi panggilan konstruktor.

parameterTypes
Type[]

Jenis parameter konstruktor.

requiredCustomModifiers
Type[][]

Array jenis array. Setiap array jenis mewakili pengubah kustom yang diperlukan untuk parameter yang sesuai, seperti IsConst. Jika parameter tertentu tidak memiliki pengubah kustom yang diperlukan, tentukan null alih-alih array jenis. Jika tidak ada parameter yang memerlukan pengubah kustom, tentukan null alih-alih array array.

optionalCustomModifiers
Type[][]

Array jenis array. Setiap array jenis mewakili pengubah kustom opsional untuk parameter yang sesuai, seperti IsConst. Jika parameter tertentu tidak memiliki pengubah kustom opsional, tentukan null alih-alih array jenis. Jika tidak ada parameter yang memiliki pengubah kustom opsional, tentukan null alih-alih array array.

Mengembalikan

Konstruktor yang ditentukan.

Atribut

Pengecualian

Ukuran requiredCustomModifiers atau optionalCustomModifiers tidak sama dengan ukuran parameterTypes.

Jenis sebelumnya dibuat menggunakan CreateType().

-atau-

Untuk jenis dinamis saat ini, IsGenericType properti adalah true, tetapi IsGenericTypeDefinition propertinya adalah false.

Keterangan

Kelebihan beban ini disediakan untuk perancang kompilator terkelola.

Catatan

Untuk informasi selengkapnya tentang pengubah kustom, lihat ECMA C# dan Standar Infrastruktur Bahasa Umum dan ECMA-335 Standar - Infrastruktur Bahasa Umum (CLI).

Berlaku untuk