Type.GetConstructor Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri az aktuális Typekonstruktort.
Túlterhelések
| Name | Description |
|---|---|
| GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
Olyan konstruktort keres, amelynek paraméterei megfelelnek a megadott argumentumtípusoknak és módosítóknak a megadott kötési megkötések és a megadott hívási konvenciók használatával. |
| GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) |
Olyan konstruktort keres, amelynek paraméterei megfelelnek a megadott argumentumtípusoknak és módosítóknak a megadott kötési megkötések használatával. |
| GetConstructor(BindingFlags, Type[]) |
Olyan konstruktort keres, amelynek paraméterei megfelelnek a megadott argumentumtípusoknak a megadott kötési megkötések használatával. |
| GetConstructor(Type[]) |
Olyan nyilvános példánykonstruktort keres, amelynek paraméterei megegyeznek a megadott tömb típusával. |
GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
Olyan konstruktort keres, amelynek paraméterei megfelelnek a megadott argumentumtípusoknak és módosítóknak a megadott kötési megkötések és a megadott hívási konvenciók használatával.
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
Paraméterek
- bindingAttr
- BindingFlags
A számbavételi értékek bitenkénti kombinációja, amely meghatározza a keresés menetét.
-vagy-
Default visszatéréshez null.
- binder
- Binder
Olyan objektum, amely tulajdonságokat határoz meg, és engedélyezi a kötést, amely magában foglalhatja egy túlterhelt metódus kiválasztását, az argumentumtípusok kényszerítését és a tag meghívását a tükröződésen keresztül.
-vagy-
Nullhivatkozás (Nothing Visual Basic) a DefaultBinder használatához.
- callConvention
- CallingConventions
Az az objektum, amely meghatározza az argumentumok sorrendjére és elrendezésére, a visszatérési érték átadásának módjára, az argumentumokhoz használt regiszterekre vonatkozó szabályokat, és a vermet megtisztítja.
- types
- Type[]
Objektumtömb Type , amely a konstruktor lekéréséhez szükséges paraméterek számát, sorrendjét és típusát jelöli.
-vagy-
A típus Type üres tömbje (azaz Type[] types = new Type[0]) egy olyan konstruktor lekéréséhez, amely nem vesz fel paramétereket.
- modifiers
- ParameterModifier[]
A tömb megfelelő eleméhez ParameterModifier társított attribútumokat képviselő objektumtömbtypes. Az alapértelmezett iratgyűjtő nem dolgozza fel ezt a paramétert.
Válaszok
A konstruktort képviselő objektum, amely megfelel a megadott követelményeknek, ha megtalálható; egyéb esetben. null
Megvalósítás
- Attribútumok
Kivételek
types többdimenziós.
-vagy-
modifiers többdimenziós.
-vagy-
types és modifiers nem azonos hosszúságúak.
Példák
Az alábbi példa beolvasja a típust MyClass, lekéri az ConstructorInfo objektumot, és megjeleníti a konstruktor aláírását.
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
Megjegyzések
Bár az alapértelmezett kötő nem dolgozza fel a ParameterModifier (a modifiers paramétert), az absztrakt System.Reflection.Binder osztály használatával írhat egy egyedi kötőt, amely feldolgozza a modifiers.
ParameterModifier csak com-interop-on keresztüli híváskor használatos, és csak a hivatkozás által átadott paramétereket kezeli a rendszer.
Ha nem létezik pontos egyezés, a binder rendszer megkísérli kényszeríteni a tömbben megadott paramétertípusokat egyezés types kiválasztásához. Ha a binder függvény nem tud kiválasztani egyezést, akkor null a függvény visszaadja.
A következő BindingFlags szűrőjelölők segítségével határozhatja meg, hogy mely konstruktorokat vegye fel a keresésbe:
A visszatéréshez meg kell adnia vagy a
BindingFlags.Instancevagy aBindingFlags.Static.Adja meg
BindingFlags.Public, hogy a nyilvános konstruktorok is szerepeljenek a keresésben.Adja meg
BindingFlags.NonPublic, hogy nem nyilvános konstruktorokat (azaz privát, belső és védett konstruktorokat) is belefoglaljon a keresésbe.
További információért lásd a System.Reflection.BindingFlags jelű részt.
Az osztály inicializálójának (statikus konstruktornak) ezzel a módszerrel történő lekéréséhez meg kell adnia BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic). Az osztály inicializálója a TypeInitializer tulajdonság használatával is lekérhető.
Az alábbi táblázat azt mutatja be, hogy egy alaposztály mely tagjait adják vissza a metódusok egy Get típuson való tükrözéskor.
| Tagtípus | Static | Nem statikus |
|---|---|---|
| Constructor | No | No |
| Field | No | Yes. A mezők mindig név és aláírás alapján rejthetők el. |
| Esemény | Nem alkalmazható | A gyakori típusrendszerszabály az, hogy az öröklés megegyezik a tulajdonságot megvalósító metódusok öröklésével. A tükröződés a tulajdonságokat név és aláírás elrejtéseként kezeli. Lásd az alábbi 2. megjegyzést. |
| Módszer | No | Yes. A metódusok (mind a virtuális, mind a nem virtuális) elrejthetők név szerint vagy elrejthetők név és aláírás alapján. |
| Beágyazott típus | No | No |
| Ingatlan | Nem alkalmazható | A gyakori típusrendszerszabály az, hogy az öröklés megegyezik a tulajdonságot megvalósító metódusok öröklésével. A tükröződés a tulajdonságokat név és aláírás elrejtéseként kezeli. Lásd az alábbi 2. megjegyzést. |
A név és aláírás szerinti elrejtés az aláírás összes részét figyelembe veszi, beleértve az egyéni módosítókat, a visszatérési típusokat, a paramétertípusokat, a sentineleket és a nem kezelt hívási konvenciókat. Ez egy bináris összehasonlítás.
A reflexió során a tulajdonságok és események név és szignatúra szerint vannak elrejtve. Ha az alaposztálynak van egy beolvasási és egy beállítási kiegészítővel rendelkező tulajdonsága, de a származtatott osztálynak csak beolvasási kiegészítője van, a származtatott osztály tulajdonsága elrejti az alaposztály tulajdonságát, és nem tudja majd elérni a beállítót az alaposztályban.
Az egyéni attribútumok nem részei a gyakori típusrendszernek.
Note
Konstruktorok és metódusok keresésekor nem hagyhat ki paramétereket. Csak invokáláskor hagyhat ki paramétereket.
Ha a jelenlegi Type egy létrehozott általános típust jelöl, ez a metódus a ConstructorInfo-t adja vissza, ahol a típusparamétereket a megfelelő típusargumentumokkal helyettesítettük. Ha az aktuális Type érték egy típusparamétert jelöl egy általános vagy általános metódus definíciójában, ez a metódus mindig ad vissza null.
Lásd még
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
A következőre érvényes:
GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
Olyan konstruktort keres, amelynek paraméterei megfelelnek a megadott argumentumtípusoknak és módosítóknak a megadott kötési megkötések használatával.
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
Paraméterek
- bindingAttr
- BindingFlags
A számbavételi értékek bitenkénti kombinációja, amely meghatározza a keresés menetét.
-vagy-
Default visszatéréshez null.
- binder
- Binder
Olyan objektum, amely tulajdonságokat határoz meg, és engedélyezi a kötést, amely magában foglalhatja egy túlterhelt metódus kiválasztását, az argumentumtípusok kényszerítését és a tag meghívását a tükröződésen keresztül.
-vagy-
Nullhivatkozás (Nothing Visual Basic) a DefaultBinder használatához.
- types
- Type[]
Objektumtömb Type , amely a konstruktor lekéréséhez szükséges paraméterek számát, sorrendjét és típusát jelöli.
-vagy-
A típus Type üres tömbje (azaz Type[] types = new Type[0]) egy olyan konstruktor lekéréséhez, amely nem vesz fel paramétereket.
-vagy-
- modifiers
- ParameterModifier[]
A paramétertípus-tömb megfelelő eleméhez társított attribútumokat képviselő objektumtömb ParameterModifier . Az alapértelmezett iratgyűjtő nem dolgozza fel ezt a paramétert.
Válaszok
A ConstructorInfo konstruktort képviselő objektum, amely megfelel a megadott követelményeknek, ha megtalálható; ellenkező esetben null.
Megvalósítás
- Attribútumok
Kivételek
types többdimenziós.
-vagy-
modifiers többdimenziós.
-vagy-
types és modifiers nem azonos hosszúságúak.
Példák
Az alábbi példa beolvasja a típust MyClass, lekéri az ConstructorInfo objektumot, és megjeleníti a konstruktor aláírását.
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
Megjegyzések
Ha nem létezik pontos egyezés, a binder rendszer megkísérli kényszeríteni a tömbben megadott paramétertípusokat egyezés types kiválasztásához. Ha a binder függvény nem tud kiválasztani egyezést, akkor null a függvény visszaadja.
A következő BindingFlags szűrőjelölők segítségével határozhatja meg, hogy mely konstruktorokat vegye fel a keresésbe:
A visszatéréshez meg kell adnia vagy a
BindingFlags.Instancevagy aBindingFlags.Static.Adja meg
BindingFlags.Public, hogy a nyilvános konstruktorok is szerepeljenek a keresésben.Adja meg
BindingFlags.NonPublic, hogy nem nyilvános konstruktorokat (azaz privát, belső és védett konstruktorokat) is belefoglaljon a keresésbe.
További információért lásd a System.Reflection.BindingFlags jelű részt.
Az osztály inicializálójának (statikus konstruktornak) ezzel a metódussal történő túlterheléséhez meg kell adnia BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic). Az osztály inicializálója a TypeInitializer tulajdonság használatával is lekérhető.
Note
Konstruktorok és metódusok keresésekor nem hagyhat ki paramétereket. Csak invokáláskor hagyhat ki paramétereket.
Ha a jelenlegi Type egy létrehozott általános típust jelöl, ez a metódus a ConstructorInfo-t adja vissza, ahol a típusparamétereket a megfelelő típusargumentumokkal helyettesítettük. Ha az aktuális Type érték egy típusparamétert jelöl egy általános vagy általános metódus definíciójában, ez a metódus mindig ad vissza null.
Lásd még
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
A következőre érvényes:
GetConstructor(BindingFlags, Type[])
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
Olyan konstruktort keres, amelynek paraméterei megfelelnek a megadott argumentumtípusoknak a megadott kötési megkötések használatával.
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
Paraméterek
- bindingAttr
- BindingFlags
A számbavételi értékek bitenkénti kombinációja, amely meghatározza a keresés menetét.
-or- Alapértelmezett visszatérési érték null.
- types
- Type[]
Típusobjektumok tömbje, amely a konstruktor lekéréséhez szükséges paraméterek számát, sorrendjét és típusát jelöli. -vagy- A típus Type üres tömbje (azaz Type[] type = Array.Empty{Type}()) egy olyan konstruktor lekéréséhez, amely nem vesz fel paramétereket. -vagy- EmptyTypes.
Válaszok
A ConstructorInfo konstruktort képviselő objektum, amely megfelel a megadott követelményeknek, ha megtalálható; ellenkező esetben null.
- Attribútumok
A következőre érvényes:
GetConstructor(Type[])
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
- Forrás:
- Type.cs
Olyan nyilvános példánykonstruktort keres, amelynek paraméterei megegyeznek a megadott tömb típusával.
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
Paraméterek
- types
- Type[]
Objektumtömb Type , amely a kívánt konstruktor paramétereinek számát, sorrendjét és típusát jelöli.
-vagy-
Üres objektumtömb Type , amely olyan konstruktort kap, amely nem vesz fel paramétereket. Az ilyen üres tömböket a static mező EmptyTypesadja meg.
Válaszok
A nyilvános példány konstruktorát képviselő objektum, amelynek paraméterei megfelelnek a paramétertípus-tömb típusoknak, ha találhatók; egyéb esetben. null
Megvalósítás
- Attribútumok
Kivételek
types többdimenziós.
Példák
Az alábbi példa beolvasja a típust MyClass, lekéri az ConstructorInfo objektumot, és megjeleníti a konstruktor aláírását.
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
Megjegyzések
Ez a metódus túlterhelés nyilvános példány konstruktorokat keres, és nem használható osztály inicializáló (statikus konstruktor) beszerzésére. Osztály inicializáló beszerzéséhez használjon túlterhelést, amely BindingFlags, és adja meg BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic). Az osztály inicializálója a TypeInitializer tulajdonság használatával is lekérhető.
Ha a kért konstruktor nem nyilvános, ez a metódus ad vissza null.
Note
Konstruktorok és metódusok keresésekor nem hagyhat ki paramétereket. Csak invokáláskor hagyhat ki paramétereket.
Ha a jelenlegi Type egy létrehozott általános típust jelöl, ez a metódus a ConstructorInfo-t adja vissza, ahol a típusparamétereket a megfelelő típusargumentumokkal helyettesítettük. Ha az aktuális Type érték egy típusparamétert jelöl egy általános vagy általános metódus definíciójában, ez a metódus mindig ad vissza null.
Lásd még
- ConstructorInfo
- DefaultBinder
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()