Type.GetConstructor Yöntem

Tanım

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

Aşırı Yüklemeler

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ştiricileriyle 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[])

Source:
Type.cs
Source:
Type.cs
Source:
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ştiricileriyle 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);
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

Parametreler

bindingAttr
BindingFlags

Aramanın nasıl yürütüleceğini belirten numaralandırma değerlerinin bit düzeyinde birleşimi.

-veya-

Default öğesini döndürmek nulliçin .

binder
Binder

Aşırı yüklenmiş yöntem seçimi, bağımsız değişken türlerinin uygulatılması ve yansıtma yoluyla bir üyenin çağrılması işlemlerini içerebilen; özellikler kümesini tanımlayan ve bağlama işlemine olanak veren bir nesne.

-veya-

kullanmak için null başvuru (Nothing Visual Basic'te DefaultBinder).

callConvention
CallingConventions

Bağımsız değişkenlerin sırası ve düzeni, dönüş değerinin geçiriliş şekli, bağımsız değişkenler için hangi yazmaçların kullanıldığı ve yığın temizlendiğinde kullanılacak kural kümesini belirten nesne.

types
Type[]

Oluşturucunun Type alabilmesi için 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 değeridir.

-veya-

içindeki 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 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

Açıklamalar

Varsayılan bağlayıcı işlemese ParameterModifier de ( modifiers parametresi), soyut System.Reflection.Binder sınıfını kullanarak işlemini modifiersgerçekleştiren özel bir bağlayıcı yazabilirsiniz. ParameterModifier yalnızca COM birlikte çalışma aracılığıyla çağrılırken kullanılır ve yalnızca başvuru tarafından geçirilen parametreler işlenir.

Tam eşleşme yoksa, binder bir eşleşme seçmek için dizide types belirtilen parametre türlerini zorlamayı dener. binder 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 veya BindingFlags.Static belirtmelisinizBindingFlags.Instance.

  • 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 edinmek için bkz. System.Reflection.BindingFlags.

Bu yöntemi kullanarak sınıf başlatıcısını (statik oluşturucu) almak için belirtmelisiniz BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic'te). Ö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ın Get üyeleri gösterilmektedir.

Üye Türü Statik Statik Olmayan
Oluşturucu Hayır Hayır
Alan Hayır Evet. Bir alan her zaman "ada ve imzaya göre gizle" özelliğindedir.
Olay Uygulanamaz Ortak tür sistemi kuralı, devralma işleminin özelliği uygulayan yöntemlerinkiyle aynı olmasıdır. Yansıma, özellikleri "ada ve imzaya göre gizle" şeklinde kullanır. Aşağıdaki 2. nota bakın.
Yöntem Hayır Evet. Bir yöntem (sanal ve sanal olmayan) "ada göre gizle" veya "ada ve imzaya göre gizle" özelliğinde olabilir.
İç İçe Tür Hayır Hayır
Özellik Uygulanamaz Ortak tür sistemi kuralı, devralma işleminin özelliği uygulayan yöntemlerinkiyle aynı olmasıdır. Yansıma, özellikleri "ada ve imzaya göre gizle" şeklinde kullanır. Aşağıdaki 2. nota bakın.
  1. "Ada ve imzaya göre gizle" özelliği, imzanın tüm parçalarını (özel değiştiriciler, dönüş türleri, parametre türleri, başlangıç/bitiş simgeleri ve yönetilmeyen çağrı kuralları dahil) dikkate alır. Bu ikili bir karşılaştırmadır.

  2. Yansıma için, özellikler ve olaylar "ada ve imzaya göre gizle" özelliğindedir. Bu temel sınıfta hem alma hem de ayarlama erişimcisine sahip bir özelliğiniz varsa, ancak türetilen sınıfın yalnızca bir alma erişimcisi varsa, türetilen sınıf özelliği temel sınıf özelliğini gizler ve siz de temel sınıfta ayarlayıcıya erişemezsiniz.

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

Not

Parametreleri, oluşturucular ve yöntemler ararken atlayamazsınız. Parametreleri yalnızca çağırırken atlayabilirsiniz.

Geçerli Type , yapılandırılmış bir genel türü temsil ederse, bu yöntem türü parametreleri uygun tür bağımsız değişkenleriyle değiştirilerek değerini döndürür ConstructorInfo . Geçerli Type , genel bir tür veya genel yöntemin tanımındaki bir 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[])

Source:
Type.cs
Source:
Type.cs
Source:
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);
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

Parametreler

bindingAttr
BindingFlags

Aramanın nasıl yürütüleceğini belirten numaralandırma değerlerinin bit düzeyinde birleşimi.

-veya-

Default öğesini döndürmek nulliçin .

binder
Binder

Aşırı yüklenmiş yöntem seçimi, bağımsız değişken türlerinin uygulatılması ve yansıtma yoluyla bir üyenin çağrılması işlemlerini içerebilen; özellikler kümesini tanımlayan ve bağlama işlemine olanak veren bir nesne.

-veya-

kullanmak için null başvuru (Nothing Visual Basic'te DefaultBinder).

types
Type[]

Oluşturucunun Type alabilmesi için 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 Bulunursa belirtilen gereksinimlerle eşleşen oluşturucuyu temsil eden bir nesne; aksi takdirde. null

Uygulamalar

Öznitelikler

Özel durumlar

types, null değeridir.

-veya-

içindeki 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 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

Açıklamalar

Tam eşleşme yoksa, binder bir eşleşme seçmek için dizide types belirtilen parametre türlerini zorlamayı dener. binder 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 veya BindingFlags.Static belirtmelisinizBindingFlags.Instance.

  • 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 edinmek 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 belirtmelisiniz BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic'te). Özelliğini kullanarak sınıf başlatıcısını TypeInitializer da alabilirsiniz.

Not

Parametreleri, oluşturucular ve yöntemler ararken atlayamazsınız. Parametreleri yalnızca çağırırken atlayabilirsiniz.

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

Ayrıca bkz.

Şunlara uygulanır

GetConstructor(BindingFlags, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
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);
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

Parametreler

bindingAttr
BindingFlags

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

types
Type[]

Oluşturucunun alabilmesi için 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 Bulunursa belirtilen gereksinimlerle eşleşen oluşturucuyu temsil eden bir nesne; aksi takdirde. null

Şunlara uygulanır

GetConstructor(Type[])

Source:
Type.cs
Source:
Type.cs
Source:
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);
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

Parametreler

types
Type[]

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

-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 değeridir.

-veya-

içindeki 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 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

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 , ve belirten BindingFlags.Static | BindingFlags.NonPublic bir aşırı yükleme BindingFlagskullanın (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic'te). Özelliğini kullanarak sınıf başlatıcısını TypeInitializer da alabilirsiniz.

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

Not

Parametreleri, oluşturucular ve yöntemler ararken atlayamazsınız. Parametreleri yalnızca çağırırken atlayabilirsiniz.

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

Ayrıca bkz.

Şunlara uygulanır