次の方法で共有


Type.GetConstructor メソッド

定義

現在の Typeの特定のコンストラクターを取得します。

オーバーロード

名前 説明
GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

指定したバインディング制約と指定した呼び出し規則を使用して、指定した引数の型と修飾子と一致するパラメーターを持つコンストラクターを検索します。

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

指定したバインド制約を使用して、指定した引数の型と修飾子と一致するパラメーターを持つコンストラクターを検索します。

GetConstructor(BindingFlags, Type[])

指定したバインド制約を使用して、指定した引数の型と一致するパラメーターを持つコンストラクターを検索します。

GetConstructor(Type[])

指定した配列内の型と一致するパラメーターを持つパブリック インスタンス コンストラクターを検索します。

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

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定したバインディング制約と指定した呼び出し規則を使用して、指定した引数の型と修飾子と一致するパラメーターを持つコンストラクターを検索します。

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

パラメーター

bindingAttr
BindingFlags

検索の実行方法を指定する列挙値のビットごとの組み合わせ。

-又は-

Default nullを返します。

binder
Binder

一連のプロパティを定義し、バインディングを有効にするオブジェクト。これには、オーバーロードされたメソッドの選択、引数型の強制型の強制、リフレクションによるメンバーの呼び出しが含まれる場合があります。

-又は-

DefaultBinderを使用するための null 参照 (Visual Basic のNothing)。

callConvention
CallingConventions

引数の順序とレイアウト、戻り値の受け渡し方法、引数に使用されるレジスタ、スタックのクリーンアップに関して使用する規則のセットを指定するオブジェクト。

types
Type[]

取得するコンストラクターのパラメーターの数、順序、および型を表す Type オブジェクトの配列。

-又は-

パラメーターを受け取っていないコンストラクターを取得 Type 型の空の配列 (つまり、Type[] 型 = 新しい Type[0])。

modifiers
ParameterModifier[]

types配列内の対応する要素に関連付けられた属性を表すParameterModifier オブジェクトの配列。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

指定した要件に一致するコンストラクターを表すオブジェクト (見つかった場合)。それ以外の場合は null

実装

属性

例外

typesnullです。

-又は-

typesの要素の 1 つがnullです。

types は多次元です。

-又は-

modifiers は多次元です。

-又は-

typesmodifiers の長さが同じではありません。

次の例では、 MyClassの型を取得し、 ConstructorInfo オブジェクトを取得し、コンストラクターシグネチャを表示します。

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 ( modifiers パラメーター) を処理しませんが、抽象 System.Reflection.Binder クラスを使用して、 modifiersを処理するカスタム バインダーを記述できます。 ParameterModifier は COM 相互運用機能を介して呼び出すときにのみ使用され、参照渡しのパラメーターのみが処理されます。

完全一致が存在しない場合、 binder は一致を選択するために、 types 配列で指定されたパラメーター型の強制を試みます。 binderが一致するものを選択できない場合は、nullが返されます。

次の BindingFlags フィルター フラグを使用して、検索に含めるコンストラクターを定義できます。

  • 戻り値を取得するには、 BindingFlags.Instance または BindingFlags.Static を指定する必要があります。

  • 検索にパブリック コンストラクターを含める BindingFlags.Public を指定します。

  • BindingFlags.NonPublicを指定して、パブリックでないコンストラクター (つまり、プライベート、内部、および保護されたコンストラクター) を検索に含めます。

詳細については、System.Reflection.BindingFlags を参照してください。

このメソッドを使用してクラス初期化子 (静的コンストラクター) を取得するには、 BindingFlags.Static | BindingFlags.NonPublic (Visual Basic でBindingFlags.StaticOrBindingFlags.NonPublic ) を指定する必要があります。 TypeInitializer プロパティを使用してクラス初期化子を取得することもできます。

次の表は、型に反映するときに、 Get メソッドによって返される基底クラスのメンバーを示しています。

メンバーの種類 スタティック 非静的
コンストラクター いいえ いいえ
フィールド いいえ Yes. フィールドは常に名前と署名によって非表示になります。
イベント 適用なし 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。
メソッド いいえ Yes. メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。
入れ子にされた型 いいえ いいえ
財産 適用なし 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。
  1. 名前と署名による隠蔽は、カスタム修飾子、戻り値の型、パラメーター型、センチネル、そしてアンマネージド呼び出し規則を含む、署名のすべての部分を考慮します。 これはバイナリ比較です。

  2. リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。

  3. カスタム属性は、共通型システムの一部ではありません。

コンストラクターとメソッドを参照するときにパラメーターを省略することはできません。 パラメーターは、呼び出し時にのみ省略できます。

現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで ConstructorInfo を返します。 現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドは常に nullを返します。

こちらもご覧ください

適用対象

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

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定したバインド制約を使用して、指定した引数の型と修飾子と一致するパラメーターを持つコンストラクターを検索します。

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

パラメーター

bindingAttr
BindingFlags

検索の実行方法を指定する列挙値のビットごとの組み合わせ。

-又は-

Default nullを返します。

binder
Binder

一連のプロパティを定義し、バインディングを有効にするオブジェクト。これには、オーバーロードされたメソッドの選択、引数型の強制型の強制、リフレクションによるメンバーの呼び出しが含まれる場合があります。

-又は-

DefaultBinderを使用するための null 参照 (Visual Basic のNothing)。

types
Type[]

取得するコンストラクターのパラメーターの数、順序、および型を表す Type オブジェクトの配列。

-又は-

パラメーターを受け取っていないコンストラクターを取得 Type 型の空の配列 (つまり、Type[] 型 = 新しい Type[0])。

-又は-

EmptyTypes

modifiers
ParameterModifier[]

パラメーター型配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

指定した要件に一致するコンストラクターを表す ConstructorInfo オブジェクト (見つかった場合)、それ以外の場合は null

実装

属性

例外

typesnullです。

-又は-

typesの要素の 1 つがnullです。

types は多次元です。

-又は-

modifiers は多次元です。

-又は-

typesmodifiers の長さが同じではありません。

次の例では、 MyClassの型を取得し、 ConstructorInfo オブジェクトを取得し、コンストラクターシグネチャを表示します。

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.Instance または BindingFlags.Static を指定する必要があります。

  • 検索にパブリック コンストラクターを含める BindingFlags.Public を指定します。

  • BindingFlags.NonPublicを指定して、パブリックでないコンストラクター (つまり、プライベート、内部、および保護されたコンストラクター) を検索に含めます。

詳細については、System.Reflection.BindingFlags を参照してください。

このメソッド オーバーロードを使用してクラス初期化子 (静的コンストラクター) を取得するには、 BindingFlags.Static | BindingFlags.NonPublic (Visual Basic でBindingFlags.StaticOrBindingFlags.NonPublic ) を指定する必要があります。 TypeInitializer プロパティを使用してクラス初期化子を取得することもできます。

コンストラクターとメソッドを参照するときにパラメーターを省略することはできません。 パラメーターは、呼び出し時にのみ省略できます。

現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで ConstructorInfo を返します。 現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドは常に nullを返します。

こちらもご覧ください

適用対象

GetConstructor(BindingFlags, Type[])

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定したバインド制約を使用して、指定した引数の型と一致するパラメーターを持つコンストラクターを検索します。

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

パラメーター

bindingAttr
BindingFlags

検索の実行方法を指定する列挙値のビットごとの組み合わせ。 または、 nullを返す既定値。

types
Type[]

取得するコンストラクターのパラメーターの数、順序、および型を表す Type オブジェクトの配列。 または、パラメーターを受け取っていないコンストラクターを取得するために Type 型の空の配列 (つまり、Type[] 型 = Array.Empty{Type}()) です。 または、 EmptyTypes

戻り値

指定した要件に一致するコンストラクターを表す ConstructorInfo オブジェクト (見つかった場合)、それ以外の場合は null

属性

適用対象

GetConstructor(Type[])

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定した配列内の型と一致するパラメーターを持つパブリック インスタンス コンストラクターを検索します。

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

パラメーター

types
Type[]

目的のコンストラクターのパラメーターの数、順序、および型を表す Type オブジェクトの配列。

-又は-

パラメーターを受け取っていないコンストラクターを取得するための、 Type オブジェクトの空の配列。 このような空の配列は、 static フィールド EmptyTypesによって提供されます。

戻り値

パラメーター型配列内の型と一致するパラメーターが見つかった場合は、パブリック インスタンス コンストラクターを表すオブジェクト。それ以外の場合は null

実装

属性

例外

typesnullです。

-又は-

typesの要素の 1 つがnullです。

types は多次元です。

次の例では、 MyClassの型を取得し、 ConstructorInfo オブジェクトを取得し、コンストラクターシグネチャを表示します。

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

注釈

このメソッド オーバーロードはパブリック インスタンス コンストラクターを検索し、クラス初期化子 (静的コンストラクター) を取得するために使用することはできません。 クラス初期化子を取得するには、 BindingFlagsを受け取るオーバーロードを使用し、 BindingFlags.Static | BindingFlags.NonPublic (Visual Basic でBindingFlags.StaticOrBindingFlags.NonPublic ) を指定します。 TypeInitializer プロパティを使用してクラス初期化子を取得することもできます。

要求されたコンストラクターが非パブリックの場合、このメソッドは nullを返します。

コンストラクターとメソッドを参照するときにパラメーターを省略することはできません。 パラメーターは、呼び出し時にのみ省略できます。

現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで ConstructorInfo を返します。 現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドは常に nullを返します。

こちらもご覧ください

適用対象