Type.GetConstructor Metoda

Definicja

Pobiera określony konstruktor bieżącego Type .

Przeciążenia

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

Wyszukuje konstruktora, którego parametry pasują do określonych typów argumentów i modyfikatorów przy użyciu określonych ograniczeń powiązania i określonej konwencji wywoływania.

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

Wyszukuje konstruktora, którego parametry pasują do określonych typów argumentów i modyfikatorów, przy użyciu określonych ograniczeń powiązania.

GetConstructor(Type[])

Wyszukuje konstruktora wystąpienia publicznego, którego parametry pasują do typów w określonej tablicy.

GetConstructor(BindingFlags, Type[])

Wyszukuje konstruktora, którego parametry pasują do określonych typów argumentów, przy użyciu określonych ograniczeń powiązania.

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

Wyszukuje konstruktora, którego parametry pasują do określonych typów argumentów i modyfikatorów przy użyciu określonych ograniczeń powiązania i określonej konwencji wywoływania.

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

Parametry

bindingAttr
BindingFlags

Bitowa kombinacja wartości wyliczenia, które określają sposób przeprowadzania wyszukiwania.

-lub- Default , aby zwrócić null .

binder
Binder

Obiekt, który określa zestaw właściwości i umożliwia powiązanie, które może obejmować wybór metody przeciążonej, wymuszanie typów argumentu i wywołanie elementu członkowskiego przez odbicie.

-lub- Odwołanie o wartości null ( Nothing w Visual Basic), aby użyć DefaultBinder .

callConvention
CallingConventions

Obiekt określający zestaw reguł do użycia w zakresie kolejności i układu argumentów, sposobu przekazywanych wartości zwracanych, rejestrów używanych dla argumentów oraz czyszczenia stosu.

types
Type[]

Tablica Type obiektów reprezentujących liczbę, kolejność i typ parametrów do uzyskania przez konstruktor.

-lub- Pusta tablica typu Type (typ [] typy = nowy typ[0]) do uzyskania konstruktora, który nie przyjmuje żadnych parametrów.

modifiers
ParameterModifier[]

Tablica ParameterModifier obiektów reprezentujących atrybuty skojarzone z odpowiednim elementem w types tablicy. Domyślny konsolidator nie przetwarza tego parametru.

Zwraca

ConstructorInfo

Obiekt reprezentujący konstruktora, który spełnia określone wymagania, jeśli został znaleziony; w przeciwnym razie null .

Implementuje

Atrybuty

Wyjątki

types to null.

-lub- Jednym z elementów w types programie jest null .

Parametr types jest wielowymiarowy.

-lub- Parametr modifiers jest wielowymiarowy.

-lub- types i modifiers nie mają tej samej długości.

Przykłady

Poniższy przykład uzyskuje typ MyClass , pobiera ConstructorInfo obiekt i wyświetla podpis konstruktora.

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 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 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 MyClass1 that is a public " +
                    "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            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: " + 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);
        }
    }
}
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

Uwagi

Mimo że domyślny binder nie przetwarza (parametru ), można użyć klasy abstrakcyjnej do napisania niestandardowego ParameterModifier modifiers klasy System.Reflection.Binder binder, który przetwarza klasę modifiers . ParameterModifier Jest używany tylko podczas wywoływania za pośrednictwem międzyplatopii COM i obsługiwane są tylko parametry, które są przekazywane przez odwołanie.

Jeśli dokładne dopasowanie nie istnieje, polecenie podejmie próbę wywłaszczania typów parametrów określonych w tablicy w celu binder types wybrania dopasowania. Jeśli polecenie binder nie może wybrać dopasowania, null zwracany jest typ .

Następujące BindingFlags flagi filtru mogą służyć do definiowania konstruktorów do dołączyć do wyszukiwania:

  • Aby uzyskać zwrot, należy określić wartość lub BindingFlags.Instance BindingFlags.Static .

  • Określ, BindingFlags.Public aby uwzględnić publiczne konstruktory w wyszukiwaniu.

  • Określ, aby uwzględnić w wyszukiwaniu konstruktory niepublicznie (czyli konstruktory prywatne, wewnętrzne i BindingFlags.NonPublic chronione).

Aby uzyskać więcej informacji, zobacz System.Reflection.BindingFlags.

Aby uzyskać inicjator klasy (konstruktor statyczny) przy użyciu tej metody, należy określić | ( w BindingFlags.Static BindingFlags.NonPublic BindingFlags.Static Or BindingFlags.NonPublic Visual Basic). Możesz również pobrać inicjator klasy przy użyciu TypeInitializer właściwości .

W poniższej tabeli przedstawiono, jakie składowe klasy bazowej są zwracane przez metody Get podczas odzwierciedlania w typie.

Typ elementu członkowskiego Static Niestatyczna
Konstruktor Nie Nie
Pole Nie Tak. Pole jest zawsze ukryte przez nazwę i podpis.
Zdarzenie Nie dotyczy Zasadą systemu typu jest to, że dziedziczenie jest takie samo, jak w przypadku metod, które implementują właściwość. Odbicie traktuje właściwości jako ukryte przez nazwę i podpis. Patrz Uwaga 2 poniżej.
Metoda Nie Tak. Metodą (zarówno wirtualną, jak i niewirtualną) może być ukrycie przez nazwę lub przez nazwę i podpis.
Typu zagnieżdżony Nie Nie
Właściwość Nie dotyczy Zasadą systemu typu jest to, że dziedziczenie jest takie samo, jak w przypadku metod, które implementują właściwość. Odbicie traktuje właściwości jako ukryte przez nazwę i podpis. Patrz Uwaga 2 poniżej.
  1. Ukrycie przez nazwę i podpis dotyczy wszystkich części podpisu, w tym modyfikatorów niestandardowych, zwraca typy, typy parametrów, wartowników i niezarządzane konwencje wywoływania. To jest porównanie binarne.

  2. W celu odbicia właściwości i zdarzenia są ukrywane przez nazwę i podpis. Jeśli istnieje właściwość z akcesorem pobierania i ustawiania w klasie bazowej, ale odziedziczona klasa ma tylko akcesor pobierania, właściwość klasy odziedziczonej ukrywa właściwości klasy bazowej, a nie można uzyskać dostępu do metody ustawiającej w klasie bazowej.

  3. Atrybuty niestandardowe nie są częścią wspólnego typu systemowego.

Uwaga

Nie można pominąć parametrów przy wyszukiwaniu konstruktorów i metod. Parametry można pominąć jedynie podczas wywoływania.

Jeśli bieżący reprezentuje skonstruowany typ ogólny, ta metoda zwraca wartość z parametrami typu zastąpionymi przez Type ConstructorInfo odpowiednie argumenty typu. Jeśli bieżący reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta Type metoda zawsze zwraca wartość null .

Zobacz też

Dotyczy

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

Wyszukuje konstruktora, którego parametry pasują do określonych typów argumentów i modyfikatorów, przy użyciu określonych ograniczeń powiązania.

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

Parametry

bindingAttr
BindingFlags

Bitowa kombinacja wartości wyliczenia, które określają sposób przeprowadzania wyszukiwania.

-lub- Default , aby zwrócić null .

binder
Binder

Obiekt, który określa zestaw właściwości i umożliwia powiązanie, które może obejmować wybór metody przeciążonej, wymuszanie typów argumentu i wywołanie elementu członkowskiego przez odbicie.

-lub- Odwołanie o wartości null ( Nothing w Visual Basic), aby użyć DefaultBinder .

types
Type[]

Tablica Type obiektów reprezentujących liczbę, kolejność i typ parametrów do uzyskania przez konstruktor.

-lub- Pusta tablica typu Type (typ [] typy = nowy typ[0]) do uzyskania konstruktora, który nie przyjmuje żadnych parametrów.

-lub- EmptyTypes.

modifiers
ParameterModifier[]

Tablica obiektów reprezentujących atrybuty skojarzone z odpowiednim ParameterModifier elementem w tablicy typów parametrów. Domyślny konsolidator nie przetwarza tego parametru.

Zwraca

ConstructorInfo

Obiekt ConstructorInfo reprezentujący konstruktor, który spełnia określone wymagania, jeśli został znaleziony; w przeciwnym razie null wartość .

Implementuje

Atrybuty

Wyjątki

types to null.

-lub- Jednym z elementów w programie types jest null .

Parametr types jest wielowymiarowy.

-lub- Parametr modifiers jest wielowymiarowy.

-lub- types i modifiers nie mają tej samej długości.

Przykłady

Poniższy przykład uzyskuje typ MyClass , pobiera ConstructorInfo obiekt i wyświetla podpis konstruktora.

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 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 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 MyClass1 that is public " +
                    "and takes an integer as a parameter is:");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            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: " + 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);
        }
    }
}
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

Uwagi

Jeśli dokładne dopasowanie nie istnieje, polecenie podejmie próbę wywłaszczania typów parametrów określonych w tablicy w celu binder types wybrania dopasowania. Jeśli polecenie binder nie może wybrać dopasowania, null zwracany jest typ .

Następujące BindingFlags flagi filtru mogą służyć do definiowania konstruktorów do dołączyć do wyszukiwania:

  • Aby uzyskać zwrot, należy określić wartość lub BindingFlags.Instance BindingFlags.Static .

  • Określ, BindingFlags.Public aby uwzględnić publiczne konstruktory w wyszukiwaniu.

  • Określ, aby uwzględnić w wyszukiwaniu konstruktory niepublicznie (czyli konstruktory prywatne, wewnętrzne i BindingFlags.NonPublic chronione).

Aby uzyskać więcej informacji, zobacz System.Reflection.BindingFlags.

Aby uzyskać inicjator klasy (konstruktor statyczny) przy użyciu tego przeciążenia metody, należy określić | BindingFlags.Static BindingFlags.NonPublic ( w BindingFlags.Static Or BindingFlags.NonPublic Visual Basic). Możesz również pobrać inicjator klasy przy użyciu TypeInitializer właściwości .

Uwaga

Nie można pominąć parametrów przy wyszukiwaniu konstruktorów i metod. Parametry można pominąć jedynie podczas wywoływania.

Jeśli bieżący reprezentuje skonstruowany typ ogólny, ta metoda zwraca typ z parametrami typu zastąpionymi odpowiednimi Type ConstructorInfo argumentami typu. Jeśli bieżący reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta Type metoda zawsze zwraca wartość null .

Zobacz też

Dotyczy

GetConstructor(Type[])

Wyszukuje konstruktora wystąpienia publicznego, którego parametry pasują do typów w określonej tablicy.

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

Parametry

types
Type[]

Tablica Type obiektów reprezentujących liczbę, kolejność i typ parametrów dla żądanego konstruktora.

-lub- Pusta tablica Type obiektów w celu uzyskania konstruktora, który nie przyjmuje żadnych parametrów. Taka pusta tablica jest dostarczana przez static pole EmptyTypes .

Zwraca

ConstructorInfo

Obiekt reprezentujący konstruktora wystąpienia publicznego, którego parametry pasują do typów w tablicy typów parametrów, jeśli został znaleziony; w przeciwnym razie null .

Implementuje

Atrybuty

Wyjątki

types to null.

-lub- Jednym z elementów w programie types jest null .

Parametr types jest wielowymiarowy.

Przykłady

Poniższy przykład uzyskuje typ MyClass , pobiera ConstructorInfo obiekt i wyświetla podpis konstruktora.

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;
using System.Security;

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);
        }
    }
}
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

Uwagi

To przeciążenie metody wyszukuje publiczne konstruktory wystąpień i nie może zostać użyte do uzyskania inicjatora klasy (konstruktor statyczny). Aby uzyskać inicjator klasy, użyj przeciążenia, które przyjmuje wartość BindingFlags i | ( w BindingFlags.Static BindingFlags.NonPublic BindingFlags.Static Or BindingFlags.NonPublic Visual Basic). Możesz również pobrać inicjator klasy przy użyciu TypeInitializer właściwości .

Jeśli żądany konstruktor nie jest publiczny, ta metoda zwraca wartość null .

Uwaga

Nie można pominąć parametrów przy wyszukiwaniu konstruktorów i metod. Parametry można pominąć jedynie podczas wywoływania.

Jeśli bieżący reprezentuje skonstruowany typ ogólny, ta metoda zwraca typ z parametrami typu zastąpionymi odpowiednimi Type ConstructorInfo argumentami typu. Jeśli bieżący reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta Type metoda zawsze zwraca wartość null .

Zobacz też

Dotyczy

GetConstructor(BindingFlags, Type[])

Wyszukuje konstruktora, którego parametry pasują do określonych typów argumentów, przy użyciu określonych ograniczeń powiązania.

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

Parametry

bindingAttr
BindingFlags

Bitowa kombinacja wartości wyliczenia, które określają sposób przeprowadzania wyszukiwania. -or — domyślnie zwraca wartość null .

types
Type[]

Tablica obiektów Typu reprezentująca liczbę, kolejność i typ parametrów do uzyskania przez konstruktor. -or — pusta tablica typu Type (typ [] typy = Array.Empty{Type}()) w celu uzyskania konstruktora, który nie przyjmuje żadnych parametrów. -lub - EmptyTypes .

Zwraca

ConstructorInfo

Obiekt ConstructorInfo reprezentujący konstruktor, który spełnia określone wymagania, jeśli został znaleziony; w przeciwnym razie null wartość .

Dotyczy