Type.GetConstructor Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá konkrétní konstruktor aktuálního Type.
Přetížení
| Name | Description |
|---|---|
| GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů a modifikátorům, pomocí zadaných omezení vazby 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 omezení vazby. |
| GetConstructor(BindingFlags, Type[]) |
Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů pomocí zadaných omezení vazby. |
| GetConstructor(Type[]) |
Vyhledá konstruktor veřejné instance, jehož parametry odpovídají typům v zadaném poli. |
GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů a modifikátorům, pomocí zadaných omezení vazby 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
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);
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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
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
Default
nullvrátit .
- binder
- Binder
Objekt, který definuje sadu vlastností a umožňuje vazbu, která může zahrnovat výběr přetížené metody, převod typů argumentů a vyvolání člena prostřednictvím reflexe.
nebo
Nulový odkaz (Nothing v jazyce Visual Basic), který se má použít DefaultBinder.
- callConvention
- CallingConventions
Objekt, který určuje sadu pravidel, která se mají použít v souvislosti s pořadím a rozložením argumentů, jak se předá vrácená hodnota, jaké registry se používají pro argumenty, a zásobník se vyčistí.
- types
- Type[]
Pole objektů představujících Type čí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í pořadač tento parametr nezpracuje.
Návraty
Objekt představující konstruktor, který odpovídá zadaným požadavkům, pokud byl nalezen; v opačném případě . null
Implementuje
- Atributy
Výjimky
types je multidimenzionální.
nebo
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 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í přes COM interop a jsou zpracovávány pouze parametry předané odkazem.
Pokud přesná shoda neexistuje, binder pokusí se provést převod typů parametrů zadaných v types poli, aby bylo možné vybrat shodu. Pokud se nepodaří binder vybrat shodu, null vrátí se.
BindingFlags Následující příznaky filtru lze použít k definování konstruktorů, které mají být zahrnuty do vyhledávání:
Abyste získali vrácení, je nutné zadat buď
BindingFlags.InstanceneboBindingFlags.Static.Zadejte
BindingFlags.Public, aby se do hledání zahrnuly veřejné konstruktory.Zadejte
BindingFlags.NonPublic, aby do vyhledávání byly zahrnuty neveřejné konstruktory (tj. soukromé, interní a chráněné konstruktory).
Další informace naleznete na System.Reflection.BindingFlags.
Chcete-li získat inicializátor třídy (statický konstruktor) pomocí 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.
Následující tabulka ukazuje, jaké členy základní třídy jsou vráceny metodami Get při reflexi typu.
| Typ členu | Statický. | Nestatické |
|---|---|---|
| Konstruktor | Ne | Ne |
| Pole | Ne | Ano. Pole se vždy skryje podle názvu a signatury. |
| Zvláštní událost | Není relevantní | Běžné pravidlo systému typů je, že dědičnost je stejná jako u metod, které implementují vlastnost. Reflexe považuje vlastnosti za skrýt podle názvu a podpisu. Viz poznámka 2 níže. |
| Metoda | Ne | Ano. Metodu (virtuální i ne virtuální) lze skrýt podle názvu nebo skrýt podle názvu a podpisu. |
| Vnořený typ | Ne | Ne |
| Vlastnictví | Není relevantní | Běžné pravidlo systému typů je, že dědičnost je stejná jako u metod, které implementují vlastnost. Reflexe považuje vlastnosti za skrýt podle názvu a podpisu. Viz poznámka 2 níže. |
Funkce skrytí podle jména a podpisu zohledňuje 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í.
Pro reflexi jsou vlastnosti a události skryty podle názvu a podpisu. Pokud máte vlastnost s oběma přístupovými metodami get i set v základní třídě, ale odvozená třída má pouze přístupovou metodu get, odvozená třída překryje vlastnost základní třídy a nebudete mít přístup k nastavovací metodě základní třídy.
Vlastní atributy nejsou součástí systému běžných typů.
Poznámka:
Při vyhledávání konstruktorů a metod nelze vynechat parametry. Parametry můžete vynechat pouze při vyvolání.
Pokud aktuální Type představuje vytvořený obecný typ, tato metoda vrátí ConstructorInfo s parametry typu nahrazené odpovídajícími argumenty typu. Pokud aktuální Type představuje parametr typu v definici obecného typu nebo obecné metody, tato metoda vždy vrátí null.
Viz také
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
Platí pro
GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů a modifikátorům, pomocí zadaných omezení vazby.
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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
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);
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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
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
Default
nullvrátit .
- binder
- Binder
Objekt, který definuje sadu vlastností a umožňuje vazbu, která může zahrnovat výběr přetížené metody, převod typů argumentů a vyvolání člena prostřednictvím reflexe.
nebo
Nulový odkaz (Nothing v jazyce Visual Basic), který se má použít DefaultBinder.
- types
- Type[]
Pole objektů představujících Type čí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.
nebo
- modifiers
- ParameterModifier[]
Pole ParameterModifier objektů představující atributy přidružené k odpovídajícímu prvku v poli typu parametru. Výchozí pořadač tento parametr nezpracuje.
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 multidimenzionální.
nebo
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 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 provést převod typů parametrů zadaných v types poli, aby bylo možné vybrat shodu. Pokud se nepodaří binder vybrat shodu, null vrátí se.
BindingFlags Následující příznaky filtru lze použít k definování konstruktorů, které mají být zahrnuty do vyhledávání:
Abyste získali vrácení, je nutné zadat buď
BindingFlags.InstanceneboBindingFlags.Static.Zadejte
BindingFlags.Public, aby se do hledání zahrnuly veřejné konstruktory.Zadejte
BindingFlags.NonPublic, aby do vyhledávání byly zahrnuty neveřejné konstruktory (tj. soukromé, interní a chráněné konstruktory).
Další informace naleznete na System.Reflection.BindingFlags.
Chcete-li získat inicializátor třídy (statický konstruktor) pomocí tohoto přetížení 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 vyhledávání konstruktorů a metod nelze vynechat parametry. Parametry můžete vynechat pouze při vyvolání.
Pokud aktuální Type představuje vytvořený obecný typ, tato metoda vrátí ConstructorInfo s parametry typu nahrazené odpovídajícími argumenty typu. Pokud aktuální Type představuje parametr typu v definici obecného typu nebo obecné metody, tato metoda vždy vrátí null.
Viz také
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
Platí pro
GetConstructor(BindingFlags, Type[])
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
Vyhledá konstruktor, jehož parametry odpovídají zadaným typům argumentů pomocí zadaných omezení vazby.
public:
System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
public System.Reflection.ConstructorInfo? GetConstructor(System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
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í způsob provedení hledání.
-nebo- Výchozí hodnota, která se má vrátit null.
- types
- Type[]
Pole type objekty představující číslo, pořadí a typ parametrů pro konstruktor získat. -or- Prázdné pole typu Type (tj. 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
- Atributy
Platí pro
GetConstructor(Type[])
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- Type.cs
- Zdroj:
- 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
public System.Reflection.ConstructorInfo? GetConstructor(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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
member this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
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 objektů představujících Type číslo, pořadí a typ parametrů požadovaného konstruktoru.
nebo
Prázdné pole Type objektů pro získání konstruktoru, který nepřijímá žádné parametry. Takové prázdné pole je poskytováno static polem EmptyTypes.
Návraty
Objekt představující konstruktor veřejné instance, jehož parametry odpovídají typům v poli typů parametrů, pokud jsou nalezeny; v opačném případě . null
Implementuje
- Atributy
Výjimky
types je multidimenzionální.
Příklady
Následující příklad získá typ MyClass, získá ConstructorInfo objekt a zobrazí podpis konstruktoru.
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
Toto přetížení metody vyhledá konstruktory veřejných instancí a nelze je použít k získání inicializátoru třídy (statický konstruktor). Chcete-li získat inicializátor třídy, použijte přetížení, které přebírá BindingFlagsa určuje | 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í tato metoda null.
Poznámka:
Při vyhledávání konstruktorů a metod nelze vynechat parametry. Parametry můžete vynechat pouze při vyvolání.
Pokud aktuální Type představuje vytvořený obecný typ, tato metoda vrátí ConstructorInfo s parametry typu nahrazené odpovídajícími argumenty typu. Pokud aktuální Type představuje parametr typu v definici obecného typu nebo obecné metody, tato metoda vždy vrátí null.
Viz také
- ConstructorInfo
- DefaultBinder
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()