Type.GetConstructor Metoda

Definice

Získá konkrétní konstruktor aktuálního Type.

Přetížení

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

Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů a modifikátorům, pomocí zadaných vazeb omezení a zadané konvence volání.

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

Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů a modifikátorům, pomocí zadaných vazeb omezení.

GetConstructor(BindingFlags, Type[])

Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů pomocí zadaných vazeb omezení.

GetConstructor(Type[])

Vyhledá konstruktor veřejné instance, jehož parametry odpovídají typům v zadaném poli.

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů a modifikátorům, pomocí zadaných vazeb omezení a zadané konvence volání.

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

Bitové kombinace hodnot výčtu, které určují způsob provedení hledání.

-nebo-

Defaulta vrátí .null

binder
Binder

Objekt, který definuje sadu vlastností a umožňuje vázání, jež může zahrnovat výběr přetěžované metody, vynucení typů argumentů a vyvolání členu prostřednictvím reflexe.

-nebo-

Nulový odkaz (Nothing v jazyce Visual Basic) pro použití .DefaultBinder

callConvention
CallingConventions

Objekt, který určuje sadu pravidel, která se mají použít, pokud jde o pořadí a rozložení argumentů, způsob předání návratové hodnoty, jaké registry se používají pro argumenty a zásobník je vyčištěn.

types
Type[]

Pole Type objektů představující číslo, pořadí a typ parametrů pro konstruktor získat.

-nebo-

Prázdné pole typu Type (tj. Type[] types = new Type[0]) pro získání konstruktoru, který nepřijímá žádné parametry.

modifiers
ParameterModifier[]

Pole ParameterModifier objektů představující atributy přidružené k odpovídajícímu prvku v types poli. Výchozí vázací objekt tento parametr nezpracovává.

Návraty

Objekt představující konstruktor, který odpovídá zadaným požadavkům, pokud je nalezen; v opačném případě . null

Implementuje

Atributy

Výjimky

types je null.

-nebo-

Jedním z prvků v types souboru je null.

Pole types je multidimenzionální.

-nebo-

Pole modifiers je multidimenzionální.

-nebo-

types a modifiers nemají stejnou délku.

Příklady

Následující příklad získá typ MyClass, získá ConstructorInfo objekt a zobrazí podpis konstruktoru.

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

Poznámky

I když výchozí pořadač nezpracovává ParameterModifier ( modifiers parametr), můžete použít abstraktní System.Reflection.Binder třídu k zápisu vlastního pořadače, který zpracovává modifiers. ParameterModifier se používá pouze při volání prostřednictvím zprostředkovatele komunikace com a jsou zpracovávány pouze parametry, které jsou předány odkazem.

Pokud přesná shoda neexistuje, binder nástroj se pokusí převést typy parametrů zadané v types poli za účelem výběru shody. Pokud není binder možné vybrat shodu, null vrátí se hodnota .

Následující BindingFlags příznaky filtru lze použít k definování konstruktorů, které se mají zahrnout do vyhledávání:

  • Pokud chcete získat vrácení, musíte zadat nebo BindingFlags.InstanceBindingFlags.Static .

  • Zadejte BindingFlags.Public , chcete-li do vyhledávání zahrnout veřejné konstruktory.

  • Zadejte BindingFlags.NonPublic , aby se do hledání zahrnuly neveřejné konstruktory (tj. soukromé, interní a chráněné konstruktory).

Další informace naleznete v tématu System.Reflection.BindingFlags.

Chcete-li získat inicializátor třídy (statický konstruktor) pomocí této metody, je nutné zadat BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic v jazyce Visual Basic). Inicializátor třídy můžete získat také pomocí TypeInitializer vlastnosti .

Následující tabulka ukazuje, které členy základní třídy jsou vráceny metodami Get při reflexi typu.

Typ členu Static Nestatický
Konstruktor No No
Pole No Yes. Pole je vždy skrýváno podle názvu a podpisu.
Událost Neuvedeno Pro systém typů platí obecné pravidlo, že dědičnost je stejná jako u metod, které implementují vlastnost. Reflexe pracuje s třídami jako se skrývanými podle názvu a podpisu. Viz poznámka 2 níže.
Metoda No Yes. Metody (virtuální i nevirtuální) mohou být skrývány podle názvu nebo podle názvu a podpisu.
Vnořený typ No No
Vlastnost Neuvedeno Pro systém typů platí obecné pravidlo, že dědičnost je stejná jako u metod, které implementují vlastnost. Reflexe pracuje s třídami jako se skrývanými podle názvu a podpisu. Viz poznámka 2 níže.
  1. Skrývání podle názvu a podpisu bere v úvahu všechny části podpisu včetně vlastních modifikátorů, návratových typů, typů parametrů, sentinelů a nespravovaných konvencí volání. Jedná se o binární porovnání.

  2. Pro účely reflexe jsou vlastnosti a události skrývány podle názvu a podpisu. Má-li vlastnost v základní třídě přístupové metody get i set, ale odvozená třída má pouze přístupovou metodu get, vlastnost odvozené třídy skryje vlastnost základní třídy a nebudete mít k dispozici přístup k metodě set základní třídy.

  3. Vlastní atributy nejsou součástí systému společných typů.

Poznámka

Při hledání konstruktorů a metod nelze parametry vynechávat. Můžete je vynechat pouze při vyvolání.

Pokud aktuální Type představuje konstruovaný obecný typ, vrátí ConstructorInfo tato metoda s parametry typu nahrazené argumenty příslušného typu. Pokud current Type představuje parametr typu v definici obecného typu nebo obecné metody, tato metoda vždy vrátí null.

Viz také

Platí pro

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů a modifikátorům pomocí zadaných vazeb.

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

Bitové kombinace hodnot výčtu, které určují způsob provedení hledání.

-nebo-

Defaulta vrátí .null

binder
Binder

Objekt, který definuje sadu vlastností a umožňuje vázání, jež může zahrnovat výběr přetěžované metody, vynucení typů argumentů a vyvolání členu prostřednictvím reflexe.

-nebo-

Nulový odkaz (Nothing v jazyce Visual Basic) pro použití .DefaultBinder

types
Type[]

Pole Type objektů představující číslo, pořadí a typ parametrů pro konstruktor získat.

-nebo-

Prázdné pole typu Type (to znamená Type[] types = new Type[0]) pro získání konstruktoru, který nepřijímá žádné parametry.

-nebo-

EmptyTypes.

modifiers
ParameterModifier[]

Pole ParameterModifier objektů představující atributy přidružené k odpovídajícímu prvku pole typu parametru. Výchozí vázací objekt tento parametr nezpracovává.

Návraty

Objekt ConstructorInfo představující konstruktor, který odpovídá zadaným požadavkům, pokud je nalezen; v opačném případě null.

Implementuje

Atributy

Výjimky

types je null.

-nebo-

Jedním z prvků v types souboru je null.

Pole types je multidimenzionální.

-nebo-

Pole modifiers je multidimenzionální.

-nebo-

types a modifiers nemají stejnou délku.

Příklady

Následující příklad získá typ MyClass, získá ConstructorInfo objekt a zobrazí podpis konstruktoru.

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

Poznámky

Pokud přesná shoda neexistuje, binder pokusí se vymáhnou typy parametrů zadané v types poli, aby bylo možné vybrat shodu. Pokud není binder možné vybrat shodu, null vrátí se.

Následující BindingFlags příznaky filtru lze použít k definování konstruktorů, které se mají zahrnout do hledání:

  • Pokud chcete získat vrácení, musíte zadat buď BindingFlags.Instance nebo BindingFlags.Static .

  • Zadejte BindingFlags.Public , aby se veřejné konstruktory zahrnuly do hledání.

  • Zadejte BindingFlags.NonPublic , aby se do hledání zahrnuly neveřejné konstruktory (tj. soukromé, interní a chráněné konstruktory).

Další informace naleznete v tématu System.Reflection.BindingFlags.

Chcete-li získat inicializátor třídy (statický konstruktor) pomocí přetížení této metody, musíte zadat BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic v jazyce Visual Basic). Inicializátor třídy můžete také získat pomocí TypeInitializer vlastnosti .

Poznámka

Při hledání konstruktorů a metod nelze parametry vynechávat. Můžete je vynechat pouze při vyvolání.

Pokud proud Type představuje konstruovaný obecný typ, vrátí ConstructorInfo tato metoda hodnotu s parametry typu nahrazenými příslušnými argumenty typu. Pokud current Type představuje parametr typu v definici obecného typu nebo obecné metody, tato metoda vždy vrátí null.

Viz také

Platí pro

GetConstructor(BindingFlags, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů pomocí zadaných vazeb.

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

Bitové kombinace hodnot výčtu, které určují, jak se provádí hledání. -or- Výchozí pro vrácení null.

types
Type[]

Pole type objektů představujících číslo, pořadí a typ parametrů, které má konstruktor získat. -or- Prázdné pole typu Type (to znamená Type[] types = Array.Empty{Type}()) pro získání konstruktoru, který nepřijímá žádné parametry. -nebo- EmptyTypes.

Návraty

Objekt ConstructorInfo představující konstruktor, který odpovídá zadaným požadavkům, pokud je nalezen; v opačném případě null.

Platí pro

GetConstructor(Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Vyhledá konstruktor veřejné instance, jehož parametry odpovídají typům v zadaném poli.

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[]

Pole Type objektů představující číslo, pořadí a typ parametrů požadovaného konstruktoru.

-nebo-

Prázdné pole Type objektů, aby se získal konstruktor, který nepřijímá žádné parametry. Takové prázdné pole je poskytováno polem staticEmptyTypes.

Návraty

Objekt představující konstruktor veřejné instance, jehož parametry odpovídají typům v poli typu parametru, pokud jsou nalezeny; v opačném případě . null

Implementuje

Atributy

Výjimky

types je null.

-nebo-

Jedním z prvků v types souboru je null.

Pole types je multidimenzionální.

Příklady

Následující příklad získá typ MyClass, získá ConstructorInfo objekt a zobrazí podpis konstruktoru.

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

Poznámky

Přetížení této metody hledá konstruktory veřejné instance a nelze ho použít k získání inicializátoru třídy (statického konstruktoru). Chcete-li získat inicializátor třídy, použijte přetížení, které přebírá BindingFlagsa zadejte | BindingFlags.NonPublicBindingFlags.Static(BindingFlags.StaticOrBindingFlags.NonPublic v jazyce Visual Basic). Inicializátor třídy můžete také získat pomocí TypeInitializer vlastnosti .

Pokud je požadovaný konstruktor neveřejný, vrátí nulltato metoda .

Poznámka

Při hledání konstruktorů a metod nelze parametry vynechávat. Můžete je vynechat pouze při vyvolání.

Pokud proud Type představuje konstruovaný obecný typ, vrátí ConstructorInfo tato metoda hodnotu s parametry typu nahrazenými příslušnými argumenty typu. Pokud current Type představuje parametr typu v definici obecného typu nebo obecné metody, tato metoda vždy vrátí null.

Viz také

Platí pro