Type.GetConstructor Metode

Definisi

Mendapatkan konstruktor tertentu dari saat ini Type.

Overload

GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

Mencari konstruktor yang parameternya cocok dengan jenis argumen dan pengubah yang ditentukan, menggunakan batasan pengikatan yang ditentukan dan konvensi panggilan yang ditentukan.

GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])

Mencari konstruktor yang parameternya cocok dengan jenis argumen dan pengubah yang ditentukan, menggunakan batasan pengikatan yang ditentukan.

GetConstructor(BindingFlags, Type[])

Mencari konstruktor yang parameternya cocok dengan jenis argumen yang ditentukan, menggunakan batasan pengikatan yang ditentukan.

GetConstructor(Type[])

Mencari konstruktor instans publik yang parameternya cocok dengan jenis dalam array yang ditentukan.

GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

Sumber:
Type.cs
Sumber:
Type.cs
Sumber:
Type.cs

Mencari konstruktor yang parameternya cocok dengan jenis argumen dan pengubah yang ditentukan, menggunakan batasan pengikatan yang ditentukan dan konvensi panggilan yang ditentukan.

public:
 System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.ConstructorInfo? GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As ConstructorInfo

Parameter

bindingAttr
BindingFlags

Kombinasi bitwise dari nilai enumerasi yang menentukan bagaimana pencarian dilakukan.

-atau-

Default untuk mengembalikan null.

binder
Binder

Objek yang mendefinisikan sekumpulan properti dan memungkinkan pengikatan, yang dapat melibatkan pemilihan metode yang kelebihan beban, paksaan jenis argumen, dan pemanggilan anggota melalui refleksi.

-atau-

Referensi null (Nothing di Visual Basic), untuk menggunakan DefaultBinder.

callConvention
CallingConventions

Objek yang menentukan sekumpulan aturan yang akan digunakan mengenai urutan dan tata letak argumen, bagaimana nilai yang dikembalikan diteruskan, register apa yang digunakan untuk argumen, dan tumpukan dibersihkan.

types
Type[]

Array Type objek yang mewakili angka, urutan, dan jenis parameter untuk didapatkan konstruktor.

-atau-

Array kosong dari jenis Type (yaitu, Tipe[] jenis = Tipe baru[0]) untuk mendapatkan konstruktor yang tidak mengambil parameter.

modifiers
ParameterModifier[]

Array ParameterModifier objek yang mewakili atribut yang terkait dengan elemen yang sesuai dalam types array. Pengikat default tidak memproses parameter ini.

Mengembalikan

Objek yang mewakili konstruktor yang cocok dengan persyaratan yang ditentukan, jika ditemukan; jika tidak, null.

Penerapan

Atribut

Pengecualian

typesadalah null.

-atau-

Salah satu elemen di types adalah null.

types bersifat multidimensi.

-atau-

modifiers bersifat multidimensi.

-atau-

types Dan modifiers janganlah kamu berkutat pada jalan yang lumayan.

Contoh

Contoh berikut mendapatkan jenis MyClass, mendapatkan ConstructorInfo objek , dan menampilkan tanda tangan konstruktor.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the public instance constructor that takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, CallingConventions::HasThis, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available." );
      }
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( "ArgumentException: {0}", e->Message );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
using System;
using System.Reflection;
using System.Security;

public class MyClass3
{
    public MyClass3(int i) { }
    public static void Main()
    {
        try
        {
            Type myType = typeof(MyClass3);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            // Get the public instance constructor that takes an integer parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null,
                CallingConventions.HasThis, types, null);
            if (constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass3 that is a public " +
                    "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass3 that is a public instance " +
                    "method and takes an integer as a parameter is not available.");
            }
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch (ArgumentException e)
        {
            Console.WriteLine("ArgumentException: " + e.Message);
        }
        catch (SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}
open System
open System.Reflection
open System.Security

type MyClass1(i: int) = class end

try
    let myType = typeof<MyClass1>
    let types = [| typeof<int> |]
    // Get the public instance constructor that takes an integer parameter.
    let constructorInfoObj = myType.GetConstructor(BindingFlags.Instance ||| BindingFlags.Public, null, CallingConventions.HasThis, types, null)
    if constructorInfoObj <> null then
        printfn "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: \n{constructorInfoObj}"
    else
        printfn "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available."
with
| :? ArgumentNullException as e ->
    printfn $"ArgumentNullException: {e.Message}"
| :? ArgumentException as e ->
    printfn $"ArgumentException: {e.Message}"
| :? SecurityException as e ->
    printfn $"SecurityException: {e.Message}"
| e ->
    printfn $"Exception: {e.Message}"
Public Class MyClass1
    Public Sub New(ByVal i As Integer)
    End Sub
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Integer)
            ' Get the public instance constructor that takes an integer parameter.
            Dim constructorInfoObj As ConstructorInfo = _
                        myType.GetConstructor(BindingFlags.Instance Or _
                        BindingFlags.Public, Nothing, _
                        CallingConventions.HasThis, types, Nothing)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is not available.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As ArgumentException
            Console.WriteLine("ArgumentException: " + e.Message)
        Catch e As SecurityException
            Console.WriteLine("SecurityException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class

Keterangan

Meskipun pengikat default tidak memproses ParameterModifier ( modifiers parameter ), Anda dapat menggunakan kelas abstrak System.Reflection.Binder untuk menulis pengikat kustom yang memproses modifiers. ParameterModifier hanya digunakan saat memanggil melalui interop COM, dan hanya parameter yang diteruskan oleh referensi yang ditangani.

Jika kecocokan persis tidak ada, binder akan mencoba memaksa jenis parameter yang ditentukan dalam types array untuk memilih kecocokan. binder Jika tidak dapat memilih kecocokan, maka null dikembalikan.

Bendera filter berikut BindingFlags dapat digunakan untuk menentukan konstruktor mana yang akan disertakan dalam pencarian:

  • Anda harus menentukan salah satu BindingFlags.Instance atau BindingFlags.Static untuk mendapatkan pengembalian.

  • Tentukan BindingFlags.Public untuk menyertakan konstruktor publik dalam pencarian.

  • Tentukan BindingFlags.NonPublic untuk menyertakan konstruktor non-publik (yaitu, konstruktor privat, internal, dan terlindungi) dalam pencarian.

Lihat System.Reflection.BindingFlags untuk informasi lebih lanjut.

Untuk mendapatkan penginisialisasi kelas (konstruktor statis) menggunakan metode ini, Anda harus menentukan BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic di Visual Basic). Anda juga bisa mendapatkan penginisialisasi kelas menggunakan TypeInitializer properti .

Tabel berikut menunjukkan anggota kelas dasar apa yang dikembalikan oleh Get metode saat mencerminkan jenis.

Jenis anggota Statis Non-Statis
Konstruktor Tidak Tidak
Bidang Tidak Ya. Bidang selalu disembunyikan menurut nama dan tanda tangan.
Kejadian Tidak berlaku Aturan sistem jenis umum adalah bahwa pewarisan sama dengan metode yang mengimplementasikan properti . Pantulan memperlakukan properti sebagai hide-by-name-and-signature. Lihat catatan 2 di bawah ini.
Metode Tidak Ya. Metode (baik virtual maupun non-virtual) dapat berupa hide-by-name atau hide-by-name-and-signature.
Tipe Berlapis Tidak Tidak
Properti Tidak berlaku Aturan sistem jenis umum adalah bahwa pewarisan sama dengan metode yang mengimplementasikan properti . Pantulan memperlakukan properti sebagai hide-by-name-and-signature. Lihat catatan 2 di bawah ini.
  1. Hide-by-name-and-signature mempertimbangkan semua bagian tanda tangan, termasuk pengubah kustom, jenis pengembalian, jenis parameter, sentinel, dan konvensi panggilan yang tidak dikelola. Ini adalah perbandingan biner.

  2. Untuk refleksi, properti dan peristiwa adalah hide-by-name-and-signature. Jika Anda memiliki properti dengan aksesor get dan set di kelas dasar, tetapi kelas turunan hanya memiliki aksesor get, properti kelas turunan menyembunyikan properti kelas dasar, dan Anda tidak akan dapat mengakses setter pada kelas dasar.

  3. Atribut kustom bukan bagian dari sistem jenis umum.

Catatan

Anda tidak dapat menghilangkan parameter saat mencari konstruktor dan metode. Anda hanya dapat menghilangkan parameter saat memanggil.

Jika saat ini Type mewakili jenis generik yang dibangun, metode ini mengembalikan ConstructorInfo dengan parameter jenis yang digantikan oleh argumen jenis yang sesuai. Jika saat ini Type mewakili parameter jenis dalam definisi jenis generik atau metode generik, metode ini selalu mengembalikan null.

Lihat juga

Berlaku untuk

GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])

Sumber:
Type.cs
Sumber:
Type.cs
Sumber:
Type.cs

Mencari konstruktor yang parameternya cocok dengan jenis argumen dan pengubah yang ditentukan, menggunakan batasan pengikatan yang ditentukan.

public:
 System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.ConstructorInfo? GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As ConstructorInfo

Parameter

bindingAttr
BindingFlags

Kombinasi bitwise dari nilai enumerasi yang menentukan bagaimana pencarian dilakukan.

-atau-

Default untuk mengembalikan null.

binder
Binder

Objek yang mendefinisikan sekumpulan properti dan memungkinkan pengikatan, yang dapat melibatkan pemilihan metode yang kelebihan beban, paksaan jenis argumen, dan pemanggilan anggota melalui refleksi.

-atau-

Referensi null (Nothing di Visual Basic), untuk menggunakan DefaultBinder.

types
Type[]

Array Type objek yang mewakili angka, urutan, dan jenis parameter untuk didapatkan konstruktor.

-atau-

Array kosong dari jenis Type (yaitu, Tipe[] jenis = Tipe baru[0]) untuk mendapatkan konstruktor yang tidak mengambil parameter.

-atau-

EmptyTypes.

modifiers
ParameterModifier[]

Array ParameterModifier objek yang mewakili atribut yang terkait dengan elemen yang sesuai dalam array jenis parameter. Pengikat default tidak memproses parameter ini.

Mengembalikan

Objek ConstructorInfo yang mewakili konstruktor yang cocok dengan persyaratan yang ditentukan, jika ditemukan; jika tidak, null.

Penerapan

Atribut

Pengecualian

typesadalah null.

-atau-

Salah satu elemen di types adalah null.

types bersifat multidimensi.

-atau-

modifiers bersifat multidimensi.

-atau-

types Dan modifiers janganlah kamu berkutat pada jalan yang lumayan.

Contoh

Contoh berikut mendapatkan jenis MyClass, mendapatkan ConstructorInfo objek , dan menampilkan tanda tangan konstruktor.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the constructor that is public and takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is public and takes an integer as a parameter is:" );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of the MyClass1 that is public and takes an integer as a parameter is not available." );
      }
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( "ArgumentException: {0}", e->Message );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
using System;
using System.Reflection;
using System.Security;

public class MyClass2
{
    public MyClass2(int i) { }
    public static void Main()
    {
        try
        {
            Type myType = typeof(MyClass2);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            // Get the constructor that is public and takes an integer parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null, types, null);
            if (constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass2 that is public " +
                    "and takes an integer as a parameter is:");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of the MyClass2 that is public " +
                    "and takes an integer as a parameter is not available.");
            }
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch (ArgumentException e)
        {
            Console.WriteLine("ArgumentException: " + e.Message);
        }
        catch (SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}
open System
open System.Reflection
open System.Security

type MyClass1(i: int) = class end

try
    let myType = typeof<MyClass1>
    let types = [| typeof<int> |]
    // Get the constructor that is public and takes an integer parameter.
    let constructorInfoObj = myType.GetConstructor(BindingFlags.Instance ||| BindingFlags.Public, null, types, null)
    if constructorInfoObj <> null then
        printfn "The constructor of MyClass1 that is public and takes an integer as a parameter is:\n{constructorInfoObj}"
    else
        printfn "The constructor of the MyClass1 that is public and takes an integer as a parameter is not available."
with
| :? ArgumentNullException as e ->
    printfn $"ArgumentNullException: {e.Message}"
| :? ArgumentException as e ->
    printfn $"ArgumentException: {e.Message}"
| :? SecurityException as e ->
    printfn $"SecurityException: {e.Message}"
| e ->
    printfn $"Exception: {e.Message}"
Imports System.Reflection
Imports System.Security


Public Class MyClass1
    Public Sub New(ByVal i As Integer)
    End Sub

    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Integer)
            ' Get the constructor that is public and takes an integer parameter.
            Dim constructorInfoObj As ConstructorInfo = _
                     myType.GetConstructor(BindingFlags.Instance Or _
                     BindingFlags.Public, Nothing, types, Nothing)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass1 that is " + _
                               "public and takes an integer as a parameter is ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor of MyClass1 that is " + _
                  "public and takes an integer as a parameter is not available.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As ArgumentException
            Console.WriteLine("ArgumentException: " + e.Message)
        Catch e As SecurityException
            Console.WriteLine("SecurityException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class

Keterangan

Jika kecocokan persis tidak ada, binder akan mencoba memaksa jenis parameter yang ditentukan dalam types array untuk memilih kecocokan. binder Jika tidak dapat memilih kecocokan, maka null dikembalikan.

Bendera filter berikut BindingFlags dapat digunakan untuk menentukan konstruktor mana yang akan disertakan dalam pencarian:

  • Anda harus menentukan salah satu BindingFlags.Instance atau BindingFlags.Static untuk mendapatkan pengembalian.

  • Tentukan BindingFlags.Public untuk menyertakan konstruktor publik dalam pencarian.

  • Tentukan BindingFlags.NonPublic untuk menyertakan konstruktor non-publik (yaitu, konstruktor privat, internal, dan terlindungi) dalam pencarian.

Lihat System.Reflection.BindingFlags untuk informasi lebih lanjut.

Untuk mendapatkan penginisialisasi kelas (konstruktor statis) menggunakan metode ini kelebihan beban, Anda harus menentukan BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic di Visual Basic). Anda juga bisa mendapatkan penginisialisasi kelas menggunakan TypeInitializer properti .

Catatan

Anda tidak dapat menghilangkan parameter saat mencari konstruktor dan metode. Anda hanya dapat menghilangkan parameter saat memanggil.

Jika saat ini Type mewakili jenis generik yang dibangun, metode ini mengembalikan ConstructorInfo dengan parameter jenis yang digantikan oleh argumen jenis yang sesuai. Jika saat ini Type mewakili parameter jenis dalam definisi jenis generik atau metode generik, metode ini selalu mengembalikan null.

Lihat juga

Berlaku untuk

GetConstructor(BindingFlags, Type[])

Sumber:
Type.cs
Sumber:
Type.cs
Sumber:
Type.cs

Mencari konstruktor yang parameternya cocok dengan jenis argumen yang ditentukan, menggunakan batasan pengikatan yang ditentukan.

public:
 System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
public System.Reflection.ConstructorInfo? GetConstructor (System.Reflection.BindingFlags bindingAttr, Type[] types);
member this.GetConstructor : System.Reflection.BindingFlags * Type[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (bindingAttr As BindingFlags, types As Type()) As ConstructorInfo

Parameter

bindingAttr
BindingFlags

Kombinasi bitwise dari nilai enumerasi yang menentukan bagaimana pencarian dilakukan. -atau- Default untuk mengembalikan null.

types
Type[]

Array objek Jenis yang mewakili jumlah, urutan, dan jenis parameter untuk didapatkan konstruktor. -or- Array kosong dari jenis Type (yaitu, Tipe[] jenis = Array.Empty{Type}()) untuk mendapatkan konstruktor yang tidak mengambil parameter. -atau- EmptyTypes.

Mengembalikan

Objek ConstructorInfo yang mewakili konstruktor yang cocok dengan persyaratan yang ditentukan, jika ditemukan; jika tidak, null.

Berlaku untuk

GetConstructor(Type[])

Sumber:
Type.cs
Sumber:
Type.cs
Sumber:
Type.cs

Mencari konstruktor instans publik yang parameternya cocok dengan jenis dalam array yang ditentukan.

public:
 System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
public:
 virtual System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
public System.Reflection.ConstructorInfo? GetConstructor (Type[] types);
public System.Reflection.ConstructorInfo GetConstructor (Type[] types);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (Type[] types);
member this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
abstract member GetConstructor : Type[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructor : Type[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (types As Type()) As ConstructorInfo

Parameter

types
Type[]

Array Type objek yang mewakili jumlah, urutan, dan jenis parameter untuk konstruktor yang diinginkan.

-atau-

Array Type objek kosong, untuk mendapatkan konstruktor yang tidak mengambil parameter. Array kosong seperti static itu disediakan oleh bidang EmptyTypes.

Mengembalikan

Objek yang mewakili konstruktor instans publik yang parameternya cocok dengan jenis dalam array jenis parameter, jika ditemukan; jika tidak, null.

Penerapan

Atribut

Pengecualian

typesadalah null.

-atau-

Salah satu elemen di types adalah null.

types bersifat multidmensional.

Contoh

Contoh berikut mendapatkan jenis MyClass, mendapatkan ConstructorInfo objek , dan menampilkan tanda tangan konstruktor.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1(){}

   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the constructor that takes an integer as a parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( types );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is not available." );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception caught." );
      Console::WriteLine( "Source: {0}", e->Source );
      Console::WriteLine( "Message: {0}", e->Message );
   }
}
using System;
using System.Reflection;

public class MyClass1
{
    public MyClass1() { }
    public MyClass1(int i) { }

    public static void Main()
    {
        try
        {
            Type myType = typeof(MyClass1);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            // Get the constructor that takes an integer as a parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(types);
            if (constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass1 that takes an " +
                    "integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass1 that takes an integer " +
                    "as a parameter is not available.");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception caught.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
    }
}
type MyClass1() =
    new (i: int) = MyClass1()

try
    let myType = typeof<MyClass1>
    let types = [| typeof<int> |]
    // Get the constructor that takes an integer as a parameter.
    let constructorInfoObj = myType.GetConstructor types
    if constructorInfoObj <> null then
        printfn "The constructor of MyClass1 that takes an integer as a parameter is: \n{constructorInfoObj}"
    else
        printfn "The constructor of MyClass1 that takes an integer as a parameter is not available."
with e ->
    printfn "Exception caught."
    printfn $"Source: {e.Source}"
    printfn $"Message: {e.Message}"
Imports System.Reflection
Imports System.Security

Public Class MyClass1

    Public Sub New()
    End Sub

    Public Sub New(ByVal i As Integer)
    End Sub

    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Int32)
            ' Get the constructor that takes an integer as a parameter.
            Dim constructorInfoObj As ConstructorInfo = myType.GetConstructor(types)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass that takes an integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor of MyClass that takes no " + "parameters is not available.")
            End If

        Catch e As Exception
            Console.WriteLine("Exception caught.")
            Console.WriteLine(("Source: " + e.Source))
            Console.WriteLine(("Message: " + e.Message))
        End Try
    End Sub
End Class

Keterangan

Metode ini kelebihan beban mencari konstruktor instans publik dan tidak dapat digunakan untuk mendapatkan penginisialisasi kelas (konstruktor statis). Untuk mendapatkan penginisialisasi kelas, gunakan kelebihan beban yang mengambil BindingFlags, dan menentukan | BindingFlags.NonPublicBindingFlags.Static(BindingFlags.StaticOrBindingFlags.NonPublic di Visual Basic). Anda juga bisa mendapatkan penginisialisasi kelas menggunakan TypeInitializer properti .

Jika konstruktor yang diminta non-publik, metode ini mengembalikan null.

Catatan

Anda tidak dapat menghilangkan parameter saat mencari konstruktor dan metode. Anda hanya dapat menghilangkan parameter saat memanggil.

Jika saat ini Type mewakili jenis generik yang dibangun, metode ini mengembalikan ConstructorInfo dengan parameter jenis yang digantikan oleh argumen jenis yang sesuai. Jika saat ini Type mewakili parameter jenis dalam definisi jenis generik atau metode generik, metode ini selalu mengembalikan null.

Lihat juga

Berlaku untuk