Aracılığıyla paylaş


Type.GetConstructor Yöntem

Tanım

Geçerli Typeöğesinin belirli bir oluşturucusunu alır.

Aşırı Yüklemeler

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

Belirtilen bağlama kısıtlamalarını ve belirtilen çağırma kuralını kullanarak parametreleri belirtilen bağımsız değişken türleri ve değiştiricilerle eşleşen bir oluşturucu arar.

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

Belirtilen bağlama kısıtlamalarını kullanarak parametreleri belirtilen bağımsız değişken türleri ve değiştiricilerle eşleşen bir oluşturucu arar.

GetConstructor(BindingFlags, Type[])

Belirtilen bağlama kısıtlamalarını kullanarak parametreleri belirtilen bağımsız değişken türleriyle eşleşen bir oluşturucu arar.

GetConstructor(Type[])

Parametreleri belirtilen dizideki türlerle eşleşen bir genel örnek oluşturucu arar.

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

Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs

Belirtilen bağlama kısıtlamalarını ve belirtilen çağırma kuralını kullanarak parametreleri belirtilen bağımsız değişken türleri ve değiştiricilerle eşleşen bir oluşturucu arar.

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

Parametreler

bindingAttr
BindingFlags

Aramanın nasıl yapıldığını belirten numaralandırma değerlerinin bit düzeyinde birleşimi.

-veya-

Default öğesini döndürecek null.

binder
Binder

Aşırı yüklenmiş bir yöntemin seçilmesini, bağımsız değişken türlerinin zorlamasını ve yansıma aracılığıyla bir üyenin çağrılmasına neden olabilecek bir özellik kümesini tanımlayan ve bağlamayı etkinleştiren nesne.

-veya-

kullanmak DefaultBinderiçin null başvuru (NothingVisual Basic'te).

callConvention
CallingConventions

Bağımsız değişkenlerin sırası ve düzeni, dönüş değerinin nasıl geçirildiğini, bağımsız değişkenler için hangi yazmaçların kullanıldığını ve yığının temizlenmesiyle ilgili kullanılacak kural kümesini belirten nesne.

types
Type[]

Oluşturucunun Type alması gereken parametrelerin sayısını, sırasını ve türünü temsil eden bir nesne dizisi.

-veya-

Parametre almayan bir oluşturucu almak için türünde Type boş bir dizi (type[] types = new Type[0]).

modifiers
ParameterModifier[]

Dizideki ParameterModifier ilgili öğeyle types ilişkili öznitelikleri temsil eden bir nesne dizisi. Varsayılan bağlayıcı bu parametreyi işlemez.

Döndürülenler

Bulunursa, belirtilen gereksinimlerle eşleşen oluşturucuyu temsil eden bir nesne; aksi takdirde , null.

Uygulamalar

Öznitelikler

Özel durumlar

types, null'e eşittir.

-veya-

öğesindeki types öğelerden biri de şeklindedir null.

types çok boyutludur.

-veya-

modifiers çok boyutludur.

-veya-

types ve modifiers aynı uzunlukta değildir.

Örnekler

Aşağıdaki örnek, türünü MyClassalır, nesnesini alır ConstructorInfo ve oluşturucu imzasını görüntüler.

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

Açıklamalar

Varsayılan bağlayıcı ParameterModifier ( modifiers parametresi) işlemezken, System.Reflection.Binder işlemek için soyut modifiers sınıfını kullanarak özel bir bağlayıcı yazabilirsiniz. ParameterModifier yalnızca COM birlikte çalışması aracılığıyla yapılan çağrılarda kullanılır ve yalnızca referansla geçirilen parametreler işlenir.

Tam eşleşme yoksa, binder eşleşme seçmek için dizide types belirtilen parametre türlerini zorlamaya çalışır. binder bir eşleşme seçemezse, null döndürülür.

Aşağıdaki BindingFlags filtre bayrakları, aramaya eklenecek oluşturucuları tanımlamak için kullanılabilir:

  • İade almak için ya BindingFlags.Instance ya da BindingFlags.Static belirtmelisiniz.

  • Genel oluşturucuları aramaya dahil etmek için belirtin BindingFlags.Public .

  • Genel olmayan oluşturucuları (özel, iç ve korumalı oluşturucular) aramaya dahil etmek için belirtin BindingFlags.NonPublic .

Daha fazla bilgi için bkz. System.Reflection.BindingFlags.

Bu yöntemi kullanarak sınıf başlatıcısını (statik oluşturucu) almak için (BindingFlags.NonPublicBindingFlags.StaticOr Visual Basic'te) belirtmeniz BindingFlags.Static | BindingFlags.NonPublic gerekir. Ayrıca özelliğini kullanarak sınıf başlatıcısını TypeInitializer da alabilirsiniz.

Aşağıdaki tabloda, bir türe yansıtılırken yöntemler tarafından döndürülen temel sınıf üyeleri gösterilmektedir.

Üye Türü Static Statik Olmayan
Yapıcı Hayır Hayır
Alan Hayır Evet. Alan her zaman isim ve imzaya göre gizli olur.
Etkinlik Uygulanamaz Yaygın tür sistemi kuralı, özniteliği uygulayan yöntemlerdeki devralma ile aynıdır. Yansıma, özellikleri ada ve imzaya göre gizle olarak ele alır. Aşağıdaki 2. nota bakın.
Yöntem Hayır Evet. Bir yöntem (hem sanal hem de sanal olmayan) isim odaklı gizleme veya isim ve imza odaklı gizleme olabilir.
İç İçe Yerleştirilmiş Tür Hayır Hayır
Mülkiyet Uygulanamaz Yaygın tür sistemi kuralı, özniteliği uygulayan yöntemlerdeki devralma ile aynıdır. Yansıma, özellikleri ada ve imzaya göre gizle olarak ele alır. Aşağıdaki 2. nota bakın.
  1. Ad ve imzaya göre gizleme, özel değiştiriciler, dönüş türleri, parametre türleri, sentineller ve yönetilmeyen çağırma kuralları dahil olmak üzere, imzanın tüm parçalarını dikkate alır. Bu ikili karşılaştırmadır.

  2. Yansıma için, özellikler ve olaylar ada ve imzaya göre gizlenir. Temel sınıfta hem get hem de set erişimcisine sahip bir özelliğiniz varsa, ancak türetilen sınıfın yalnızca bir get erişimcisi varsa, türetilmiş sınıf özelliği temel sınıf özelliğini gizler ve temel sınıfta ayarlayıcıya erişemezsiniz.

  3. Özel öznitelikler ortak tür sisteminin bir parçası değildir.

Uyarı

Oluşturucuları ve yöntemleri ararken parametreleri atlayamazsınız. Yalnızca çağrılırken parametreleri atlayabilirsiniz.

Geçerli Type yapılandırılmış bir genel türü temsil ediyorsa, bu yöntem tür parametrelerini uygun tür bağımsız değişkenleriyle değiştirerek ConstructorInfo'i döndürür. Geçerli Type , genel bir tür veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem her zaman döndürür null.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs

Belirtilen bağlama kısıtlamalarını kullanarak parametreleri belirtilen bağımsız değişken türleri ve değiştiricilerle eşleşen bir oluşturucu arar.

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

Parametreler

bindingAttr
BindingFlags

Aramanın nasıl yapıldığını belirten numaralandırma değerlerinin bit düzeyinde birleşimi.

-veya-

Default öğesini döndürecek null.

binder
Binder

Aşırı yüklenmiş bir yöntemin seçilmesini, bağımsız değişken türlerinin zorlamasını ve yansıma aracılığıyla bir üyenin çağrılmasına neden olabilecek bir özellik kümesini tanımlayan ve bağlamayı etkinleştiren nesne.

-veya-

kullanmak DefaultBinderiçin null başvuru (NothingVisual Basic'te).

types
Type[]

Oluşturucunun Type alması gereken parametrelerin sayısını, sırasını ve türünü temsil eden bir nesne dizisi.

-veya-

Parametre almayan bir oluşturucu almak için türünde Type boş bir dizi (type[] types = new Type[0]).

-veya-

EmptyTypes.

modifiers
ParameterModifier[]

Parametre türü dizisindeki ParameterModifier ilgili öğeyle ilişkili öznitelikleri temsil eden bir nesne dizisi. Varsayılan bağlayıcı bu parametreyi işlemez.

Döndürülenler

ConstructorInfo Belirtilen gereksinimlerle eşleşen oluşturucuyu temsil eden bir nesne( bulunursa; değilse, null).

Uygulamalar

Öznitelikler

Özel durumlar

types, null'e eşittir.

-veya-

öğesindeki types öğelerden biri de şeklindedir null.

types çok boyutludur.

-veya-

modifiers çok boyutludur.

-veya-

types ve modifiers aynı uzunlukta değildir.

Örnekler

Aşağıdaki örnek, türünü MyClassalır, nesnesini alır ConstructorInfo ve oluşturucu imzasını görüntüler.

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

Açıklamalar

Tam eşleşme yoksa, binder eşleşme seçmek için dizide types belirtilen parametre türlerini zorlamaya çalışır. binder bir eşleşme seçemezse, null döndürülür.

Aşağıdaki BindingFlags filtre bayrakları, aramaya eklenecek oluşturucuları tanımlamak için kullanılabilir:

  • İade almak için ya BindingFlags.Instance ya da BindingFlags.Static belirtmelisiniz.

  • Genel oluşturucuları aramaya dahil etmek için belirtin BindingFlags.Public .

  • Genel olmayan oluşturucuları (özel, iç ve korumalı oluşturucular) aramaya dahil etmek için belirtin BindingFlags.NonPublic .

Daha fazla bilgi için bkz. System.Reflection.BindingFlags.

Bu yöntem aşırı yüklemesini kullanarak sınıf başlatıcısını (statik oluşturucu) almak için (BindingFlags.NonPublicBindingFlags.StaticOr Visual Basic'te) belirtmeniz BindingFlags.Static | BindingFlags.NonPublic gerekir. Ayrıca özelliğini kullanarak sınıf başlatıcısını TypeInitializer da alabilirsiniz.

Uyarı

Oluşturucuları ve yöntemleri ararken parametreleri atlayamazsınız. Yalnızca çağrılırken parametreleri atlayabilirsiniz.

Geçerli Type yapılandırılmış bir genel türü temsil ediyorsa, bu yöntem tür parametrelerini uygun tür bağımsız değişkenleriyle değiştirerek ConstructorInfo'i döndürür. Geçerli Type , genel bir tür veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem her zaman döndürür null.

Ayrıca bkz.

Şunlara uygulanır

GetConstructor(BindingFlags, Type[])

Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs

Belirtilen bağlama kısıtlamalarını kullanarak parametreleri belirtilen bağımsız değişken türleriyle eşleşen bir oluşturucu arar.

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

Parametreler

bindingAttr
BindingFlags

Aramanın nasıl yapıldığını belirten numaralandırma değerlerinin bit düzeyinde birleşimi. -veya- Varsayılan olarak döndürür null.

types
Type[]

Oluşturucunun alması gereken parametrelerin sayısını, sırasını ve türünü temsil eden Tür nesneleri dizisi. -veya- Parametre almayan bir oluşturucu almak için türünde Type boş bir dizi (type[] types = Array.Empty{Type}()). -veya- EmptyTypes.

Döndürülenler

ConstructorInfo Belirtilen gereksinimlerle eşleşen oluşturucuyu temsil eden bir nesne( bulunursa; değilse, null).

Öznitelikler

Şunlara uygulanır

GetConstructor(Type[])

Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs

Parametreleri belirtilen dizideki türlerle eşleşen bir genel örnek oluşturucu arar.

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

Parametreler

types
Type[]

İstenen oluşturucu için parametrelerin sayısını, sırasını ve türünü temsil eden bir nesne dizisi Type .

-veya-

Parametre almayan Type bir oluşturucu almak için boş bir nesne dizisi. Böyle boş bir dizi alanı EmptyTypestarafından static sağlanır.

Döndürülenler

Parametreleri, bulunursa parametre türü dizisindeki türlerle eşleşen genel örnek oluşturucuyu temsil eden bir nesne; aksi takdirde , null.

Uygulamalar

Öznitelikler

Özel durumlar

types, null'e eşittir.

-veya-

öğesindeki types öğelerden biri de şeklindedir null.

types çok boyutludur.

Örnekler

Aşağıdaki örnek, türünü MyClassalır, nesnesini alır ConstructorInfo ve oluşturucu imzasını görüntüler.

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

Açıklamalar

Bu yöntem aşırı yüklemesi genel örnek oluşturucularını arar ve sınıf başlatıcısı (statik oluşturucu) almak için kullanılamaz. Sınıf başlatıcısı almak için, öğesini alan BindingFlagsve ( Visual Basic'te)BindingFlags.NonPublicBindingFlags.StaticOr belirten BindingFlags.Static | BindingFlags.NonPublic bir aşırı yükleme kullanın. Ayrıca özelliğini kullanarak sınıf başlatıcısını TypeInitializer da alabilirsiniz.

İstenen oluşturucu genel değilse, bu yöntem döndürür null.

Uyarı

Oluşturucuları ve yöntemleri ararken parametreleri atlayamazsınız. Yalnızca çağrılırken parametreleri atlayabilirsiniz.

Geçerli Type yapılandırılmış bir genel türü temsil ediyorsa, bu yöntem tür parametrelerini uygun tür bağımsız değişkenleriyle değiştirerek ConstructorInfo'i döndürür. Geçerli Type , genel bir tür veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem her zaman döndürür null.

Ayrıca bkz.

Şunlara uygulanır