Type.GetConstructor Metoda

Definicja

Pobiera konkretny konstruktor bieżącego Typeelementu .

Przeciążenia

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

Wyszukuje konstruktor, którego parametry są zgodne z określonymi typami argumentów i modyfikatorami, przy użyciu określonych ograniczeń powiązania i określonej konwencji wywoływania.

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

Wyszukuje konstruktor, którego parametry są zgodne z określonymi typami argumentów i modyfikatorami przy użyciu określonych ograniczeń powiązania.

GetConstructor(BindingFlags, Type[])

Wyszukuje konstruktor, którego parametry są zgodne z określonymi typami argumentów, przy użyciu określonych ograniczeń powiązania.

GetConstructor(Type[])

Wyszukuje konstruktor wystąpienia publicznego, którego parametry są zgodne z typami w określonej tablicy.

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

Źródło:
Type.cs
Źródło:
Type.cs
Źródło:
Type.cs

Wyszukuje konstruktor, którego parametry są zgodne z określonymi typami argumentów i modyfikatorami, 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ć nullwartość .

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) do używania elementu DefaultBinder.

callConvention
CallingConventions

Obiekt określający zestaw reguł do użycia w odniesieniu do kolejności i układu argumentów, sposób przekazywania wartości zwracanej, jakie rejestry są używane dla argumentów, a stos jest czyszczony.

types
Type[]

Tablica Type obiektów reprezentujących liczbę, kolejność i typ parametrów konstruktora do pobrania.

-lub-

Pusta tablica typu Type (czyli typ[] = nowy typ[0]), aby uzyskać konstruktor, który nie przyjmuje parametrów.

modifiers
ParameterModifier[]

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

Zwraca

Obiekt reprezentujący konstruktor, który odpowiada określonym wymaganiom, jeśli zostanie znaleziony; w przeciwnym razie , null.

Implementuje

Atrybuty

Wyjątki

types to null.

-lub-

Jednym z elementów w pliku 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 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

Uwagi

Chociaż domyślny binder nie przetwarza ParameterModifier ( modifiers parametr), można użyć klasy abstrakcyjnej System.Reflection.Binder do zapisania niestandardowego powiązania, który przetwarza modifiers. ParameterModifier jest używany tylko podczas wywoływania międzyoperacyjności MODELU COM, a obsługiwane są tylko parametry przekazywane przez odwołanie.

Jeśli dokładne dopasowanie nie istnieje, binder program podejmie próbę zmiany typów parametrów określonych w tablicy types w celu wybrania dopasowania. Jeśli element binder nie może wybrać dopasowania, null zostanie zwrócony.

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

  • Musisz określić wartość BindingFlags.Instance lub BindingFlags.Static w celu uzyskania zwrotu.

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

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

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

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

W poniższej tabeli przedstawiono, które elementy członkowskie klasy bazowej są zwracane przez Get metody podczas odzwierciedlania typu.

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 Type reprezentuje skonstruowany typ ogólny, ta metoda zwraca ConstructorInfo parametry typu zastąpione przez odpowiednie argumenty typu. Jeśli bieżący Type reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta metoda zawsze zwraca wartość null.

Zobacz też

Dotyczy

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

Źródło:
Type.cs
Źródło:
Type.cs
Źródło:
Type.cs

Wyszukuje konstruktora, którego parametry są zgodne z określonymi typami argumentów i modyfikatorami, 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ć nullwartość .

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) do używania elementu DefaultBinder.

types
Type[]

Tablica Type obiektów reprezentujących liczbę, kolejność i typ parametrów konstruktora do pobrania.

-lub-

Pusta tablica typu Type (czyli type[] types = new Type[0]), aby uzyskać konstruktor, który nie przyjmuje żadnych parametrów.

-lub-

EmptyTypes.

modifiers
ParameterModifier[]

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

Zwraca

Obiekt ConstructorInfo reprezentujący konstruktor, który jest zgodny z określonymi wymaganiami, jeśli zostanie znaleziony; w przeciwnym razie null.

Implementuje

Atrybuty

Wyjątki

types to null.

-lub-

Jednym z elementów w pliku 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 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

Uwagi

Jeśli dokładne dopasowanie nie istnieje, binder program podejmie próbę przesunięcia typów parametrów określonych w tablicy w types celu wybrania dopasowania. Jeśli element binder nie może wybrać dopasowania, null zostanie zwrócony.

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

  • Musisz określić wartość BindingFlags.Instance lub BindingFlags.Static , aby uzyskać zwrot.

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

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

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 (BindingFlags.StaticOrBindingFlags.NonPublic w Visual Basic). Inicjator klasy można również pobrać 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 Type reprezentuje skonstruowany typ ogólny, ta metoda zwraca ConstructorInfo parametry typu zastąpione przez odpowiednie argumenty typu. Jeśli bieżący Type reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta metoda zawsze zwraca wartość null.

Zobacz też

Dotyczy

GetConstructor(BindingFlags, Type[])

Źródło:
Type.cs
Źródło:
Type.cs
Źródło:
Type.cs

Wyszukuje konstruktora, którego parametry są zgodne z określonymi typami 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 - Wartość domyślna, aby zwrócić wartość null.

types
Type[]

Tablica obiektów typu reprezentujących liczbę, kolejność i typ parametrów konstruktora do pobrania. -or — pusta tablica typu Type (czyli type[] types = Array.Empty{Type}()) w celu pobrania konstruktora, który nie przyjmuje żadnych parametrów. -or- EmptyTypes.

Zwraca

Obiekt ConstructorInfo reprezentujący konstruktor, który jest zgodny z określonymi wymaganiami, jeśli zostanie znaleziony; w przeciwnym razie null.

Dotyczy

GetConstructor(Type[])

Źródło:
Type.cs
Źródło:
Type.cs
Źródło:
Type.cs

Wyszukuje konstruktor wystąpienia publicznego, którego parametry są zgodne z typami 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 żądanego konstruktora.

-lub-

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

Zwraca

Obiekt reprezentujący konstruktor wystąpienia publicznego, którego parametry są zgodne z typami w tablicy typów parametrów, jeśli zostanie znaleziony; w przeciwnym razie , null.

Implementuje

Atrybuty

Wyjątki

types to null.

-lub-

Jednym z elementów w pliku 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;

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

Uwagi

To przeciążenie metody wyszukuje konstruktory wystąpień publicznych i nie może służyć do uzyskania inicjatora klasy (konstruktora statycznego). Aby uzyskać inicjator klasy, użyj przeciążenia, które przyjmuje BindingFlags, i określ | BindingFlags.NonPublicBindingFlags.Static(BindingFlags.StaticOrBindingFlags.NonPublic w Visual Basic). Inicjator klasy można również pobrać 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 Type reprezentuje skonstruowany typ ogólny, ta metoda zwraca ConstructorInfo parametry typu zastąpione przez odpowiednie argumenty typu. Jeśli bieżący Type reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta metoda zawsze zwraca wartość null.

Zobacz też

Dotyczy