Type.GetConstructor 메서드

정의

현재 Type의 특정 생성자를 가져옵니다.

오버로드

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

지정된 바인딩 제약 조건 및 호출 규칙을 사용하여, 지정된 인수 형식 및 한정자와 매개 변수가 일치하는 생성자를 검색합니다.

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

지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 생성자를 지정된 바인딩 제약 조건으로 검색합니다.

GetConstructor(BindingFlags, Type[])

지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 인수 형식과 일치하는 생성자를 검색합니다.

GetConstructor(Type[])

지정된 배열의 형식과 일치하는 매개 변수를 가진 public 인스턴스 생성자를 검색합니다.

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

지정된 바인딩 제약 조건 및 호출 규칙을 사용하여, 지정된 인수 형식 및 한정자와 매개 변수가 일치하는 생성자를 검색합니다.

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

매개 변수

bindingAttr
BindingFlags

검색 방법을 지정하는 열거형 값의 비트 조합입니다.

또는

null을 반환하는 Default입니다.

binder
Binder

속성 집합을 정의하고 바인딩을 활성화하는 개체입니다. 이 개체는 또한 리플렉션을 통해 오버로드된 메서드를 선택할 수 있고 인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.

또는

Nothing를 사용하려면 null 참조(Visual Basic의 경우 DefaultBinder)입니다.

callConvention
CallingConventions

인수의 순서 및 레이아웃, 반환 값의 전달 방법, 인수에 사용되는 레지스터, 스택 정리 방법 등과 관련하여 사용할 규칙을 지정하는 개체입니다.

types
Type[]

가져올 생성자에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.

또는

매개 변수를 사용하지 않는 생성자를 가져오기 위한 Type 형식의 빈 배열입니다(즉, Type[] types = new Type[0]).

modifiers
ParameterModifier[]

ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types 개체 배열입니다. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.

반환

지정된 요구 사항과 일치하는 생성자를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

특성

예외

types이(가) null인 경우

또는

types의 요소 중 하나가 null입니다.

types가 다차원 배열인 경우

또는

modifiers가 다차원 배열인 경우

또는

typesmodifiers의 길이가 동일하지 않습니다.

예제

다음 예제에서는 형식 MyClass을 가져오고 개체를 ConstructorInfo 가져오고 생성자 서명을 표시합니다.

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

설명

기본 바인더는 (매개 변수)를 처리 ParameterModifier 하지 않지만 추상 System.Reflection.Binder 클래스를 사용하여 를 처리하는 modifiers사용자 지정 바인더를 작성할 수 modifiers 있습니다. ParameterModifier 는 COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다.

정확한 일치 항목이 없는 경우 는 binder 일치 항목을 선택하기 위해 배열에 types 지정된 매개 변수 형식을 강제 변환하려고 시도합니다. 가 binder 일치 null 항목을 선택할 수 없으면 가 반환됩니다.

다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 생성자를 정의할 수 있습니다.

  • 반환을 얻으려면 또는 BindingFlags.Static 를 지정 BindingFlags.Instance 해야 합니다.

  • 검색에 공용 생성자를 포함하도록 지정 BindingFlags.Public 합니다.

  • 비공용 생성자(즉, 프라이빗, 내부 및 보호된 생성자)를 검색에 포함하도록 지정 BindingFlags.NonPublic 합니다.

자세한 내용은 System.Reflection.BindingFlags를 참조하세요.

이 메서드를 사용하여 클래스 이니셜라이저(정적 생성자)를 얻으려면 (Visual Basic에서)를BindingFlags.NonPublicBindingFlags.StaticOr 지정 BindingFlags.Static | BindingFlags.NonPublic 해야 합니다. 속성을 사용하여 클래스 이니셜라이저를 TypeInitializer 가져올 수도 있습니다.

다음 표에서는 형식을 반영할 때 메서드에서 Get 반환되는 기본 클래스의 멤버를 보여 줍니다.

멤버 형식 정적 비정적
생성자 아니요 아니요
필드 아니요 예. 필드는 항상 이름별 및 서명으로 숨겨집니다.
이벤트 적용할 수 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드와 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요.
메서드 아니요 예. 메서드(가상 및 가상이 아닌 메서드)는 이름별 숨기기 또는 이름별 숨기기 및 서명일 수 있습니다.
중첩 형식 아니요 아니요
속성 적용할 수 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드와 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요.
  1. 이름별 숨기기 및 서명은 사용자 지정 한정자, 반환 형식, 매개 변수 형식, sentinels 및 관리되지 않는 호출 규칙을 포함하여 서명의 모든 부분을 고려합니다. 이진 비교입니다.

  2. 리플렉션의 경우 속성 및 이벤트는 이름별 숨기기 및 서명입니다. 기본 클래스에 get 및 set 접근자가 모두 있는 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기며 기본 클래스의 setter에 액세스할 수 없습니다.

  3. 사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.

참고

생성자 및 메서드를 조회할 때 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.

현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 형식 매개 변수가 적절한 형식 인수로 대체된 를 반환 ConstructorInfo 합니다. 현재 Type 제네릭 형식 또는 제네릭 메서드의 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 항상 를 반환합니다 null.

추가 정보

적용 대상

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

지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 생성자를 지정된 바인딩 제약 조건으로 검색합니다.

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

매개 변수

bindingAttr
BindingFlags

검색 방법을 지정하는 열거형 값의 비트 조합입니다.

또는

null을 반환하는 Default입니다.

binder
Binder

속성 집합을 정의하고 바인딩을 활성화하는 개체입니다. 이 개체는 또한 리플렉션을 통해 오버로드된 메서드를 선택할 수 있고 인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.

또는

Nothing를 사용하려면 null 참조(Visual Basic의 경우 DefaultBinder)입니다.

types
Type[]

가져올 생성자에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.

또는

매개 변수를 사용하지 않는 생성자를 가져오기 위한 Type 형식의 빈 배열입니다(즉, Type[] types = new Type[0]).

또는

EmptyTypes.

modifiers
ParameterModifier[]

매개 변수 형식 배열의 해당 요소와 연관된 특성을 나타내는 ParameterModifier 개체의 배열입니다. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.

반환

지정된 요구 사항과 일치하는 생성자를 나타내는 ConstructorInfo 개체가 있으면 이 개체를 반환하고, 그렇지 않으면 null을 반환합니다.

구현

특성

예외

types이(가) null인 경우

또는

types의 요소 중 하나가 null입니다.

types가 다차원 배열인 경우

또는

modifiers가 다차원 배열인 경우

또는

typesmodifiers의 길이가 동일하지 않습니다.

예제

다음 예제에서는 형식 MyClass을 가져오고 개체를 ConstructorInfo 가져오고 생성자 서명을 표시합니다.

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

설명

정확한 일치 항목이 없는 경우 는 binder 일치 항목을 선택하기 위해 배열에 types 지정된 매개 변수 형식을 강제 변환하려고 시도합니다. 가 binder 일치 null 항목을 선택할 수 없는 경우 가 반환됩니다.

다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 생성자를 정의할 수 있습니다.

  • 반환을 얻으려면 또는 BindingFlags.Static 를 지정 BindingFlags.Instance 해야 합니다.

  • 검색에 공용 생성자를 포함하도록 지정 BindingFlags.Public 합니다.

  • 검색에 공용이 아닌 생성자(즉, 프라이빗, 내부 및 보호된 생성자)를 포함하도록 지정 BindingFlags.NonPublic 합니다.

자세한 내용은 System.Reflection.BindingFlags를 참조하세요.

이 메서드 오버로드를 사용하여 클래스 이니셜라이저(정적 생성자)를 얻으려면 (BindingFlags.NonPublicOrBindingFlags.StaticVisual Basic에서)을 지정 BindingFlags.Static | BindingFlags.NonPublic 해야 합니다. 속성을 사용하여 클래스 이니셜라이저를 TypeInitializer 가져올 수도 있습니다.

참고

생성자 및 메서드를 조회할 때 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.

현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 형식 매개 변수가 적절한 형식 인수로 대체된 를 반환 ConstructorInfo 합니다. 현재 Type 가 제네릭 형식 또는 제네릭 메서드의 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 항상 를 반환합니다 null.

추가 정보

적용 대상

GetConstructor(BindingFlags, Type[])

지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 인수 형식과 일치하는 생성자를 검색합니다.

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

매개 변수

bindingAttr
BindingFlags

검색 방법을 지정하는 열거형 값의 비트 조합입니다. -또는- 를 반환 null하는 기본값입니다.

types
Type[]

생성자가 가져올 매개 변수의 수, 순서 및 형식을 나타내는 Type 개체의 배열입니다. -또는- 매개 변수를 사용하지 않는 생성자를 가져오는 형식 Type 의 빈 배열(즉, Type[] types = Array.Empty{Type}())입니다. -또는- EmptyTypes.

반환

지정된 요구 사항과 일치하는 생성자를 나타내는 ConstructorInfo 개체가 있으면 이 개체를 반환하고, 그렇지 않으면 null을 반환합니다.

적용 대상

GetConstructor(Type[])

지정된 배열의 형식과 일치하는 매개 변수를 가진 public 인스턴스 생성자를 검색합니다.

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

매개 변수

types
Type[]

원하는 생성자에 대한 매개 변수의 수, 순서 및 형식을 나타내는 Type 개체의 배열입니다.

또는

매개 변수를 사용하지 않는 생성자를 가져오기 위한 Type 개체의 빈 배열입니다. 이러한 빈 배열은 static 필드 EmptyTypes에서 제공됩니다.

반환

매개 변수 형식 배열의 형식과 일치하는 매개 변수를 가진 public 인스턴스 생성자를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

특성

예외

types이(가) null인 경우

또는

types의 요소 중 하나가 null입니다.

types가 다차원 배열인 경우

예제

다음 예제에서는 형식 MyClass을 가져오고 개체를 ConstructorInfo 가져오고 생성자 서명을 표시합니다.

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

설명

이 메서드 오버로드는 공용 instance 생성자를 찾고 클래스 이니셜라이저(정적 생성자)를 가져오는 데 사용할 수 없습니다. 클래스 이니셜라이저를 얻으려면 를 사용하는 BindingFlags오버로드를 사용하고 (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic에서는 )를 지정 BindingFlags.Static | BindingFlags.NonPublic 합니다. 속성을 사용하여 클래스 이니셜라이저를 TypeInitializer 가져올 수도 있습니다.

요청된 생성자가 public이 아닌 경우 이 메서드는 를 반환합니다 null.

참고

생성자 및 메서드를 조회할 때 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.

현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 형식 매개 변수가 적절한 형식 인수로 대체된 를 반환 ConstructorInfo 합니다. 현재 Type 가 제네릭 형식 또는 제네릭 메서드의 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 항상 를 반환합니다 null.

추가 정보

적용 대상