Type.GetConstructor メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の Type の特定のコンストラクターを取得します。
オーバーロード
GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
指定したバインディング制約および指定した呼び出し規則を使用して、指定した引数の型および修飾子と一致するパラメーターが設定されているコンストラクターを検索します。 |
GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) |
指定したバインディング制約を使用して、指定した引数の型および修飾子と一致するパラメーターが設定されているコンストラクターを検索します。 |
GetConstructor(Type[]) |
指定した配列の型に一致するパラメーターが設定されているパブリック インスタンス コンストラクターを検索します。 |
GetConstructor(BindingFlags, Type[]) |
指定したバインディング制約を使用して、指定した引数の型に一致するパラメーターを持つコンストラクターを検索します。 |
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
パラメーター
- binder
- Binder
一連のプロパティを定義し、バインディングを有効にするオブジェクト。バインディングには、オーバーロードされたメソッドの選択、引数の型の強制変換、リフレクションによるメンバーの呼び出しなどが含まれます。
または
Nothing
を使用する場合は、null 参照 (Visual Basic の場合は DefaultBinder)。
- callConvention
- CallingConventions
引数の順序とレイアウト、戻り値を渡す方法、引数を格納するレジスタ、スタックのクリーンアップに関する一連の規則を指定するオブジェクト。
- types
- Type[]
取得するコンストラクターのパラメーターの数、順序、および型を表す Type オブジェクトの配列。
または パラメーターをとらないコンストラクターを取得するための、Type 型の空の配列 (Type[] types = new Type[0])。
- modifiers
- ParameterModifier[]
types
配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 既定のバインダーでは、このパラメーターは処理されません。
戻り値
指定した要件と一致するコンストラクターが存在する場合は、そのコンストラクターを表すオブジェクト。それ以外の場合は null
。
実装
- 属性
例外
例
次の例では、の型を取得し、 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 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 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 MyClass1 that is a public " +
"instance method and takes an integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
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: " + 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);
}
}
}
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 指定 BindingFlags.Static Or
BindingFlags.NonPublic する必要Visual Basic。 プロパティを使用してクラス初期化子を取得 TypeInitializer できます。
次の表は、メソッドが型に反映するときに返される基本クラスの Get
メンバーを示しています。
メンバーの型 | 静的 | 非静的 |
---|---|---|
コンストラクター | いいえ | いいえ |
フィールド | いいえ | はい。 フィールドは、常に名前と署名で隠ぺいされます。 |
Event | 適用なし | 共通型システムの規則は、継承が、プロパティを実装するメソッドと同じであることを示します。 リフレクションは、プロパティを名前で隠す、署名として扱います。 下記のメモ2を参照してください。 |
メソッド | いいえ | はい。 メソッド (仮想と非仮想の両方) は、非表示にするか、名前と署名を隠すことができます。 |
入れ子にされた型 | いいえ | いいえ |
プロパティ | 適用なし | 共通型システムの規則は、継承が、プロパティを実装するメソッドと同じであることを示します。 リフレクションは、プロパティを名前で隠す、署名として扱います。 下記のメモ2を参照してください。 |
名前による隠ぺいと署名では、カスタム修飾子、戻り値の型、パラメーターの型、sentinel、アンマネージ呼び出し規約を含む、シグネチャのすべての部分が考慮されます。 これは、バイナリ比較です。
リフレクションの場合、プロパティとイベントは、名前とシグネチャが隠ぺいされます。 基底クラスに get と set の両方のアクセサーを持つプロパティがあり、派生クラスに get アクセサーのみがある場合、派生クラスのプロパティは基底クラスのプロパティを非表示にします。基底クラスの setter にアクセスすることはできません。
カスタム属性は、共通型システムの一部ではありません。
注意
コンストラクターとメソッドを参照するときにパラメーターを省略することはできません。 パラメーターを省略できるのは、の呼び出し時だけです。
現在のが Type 構築ジェネリック型を表している場合、このメソッドは、 ConstructorInfo 適切な型引数によって置き換えられた型パラメーターを持つを返します。 現在のが Type ジェネリック型またはジェネリックメソッドの定義の型パラメーターを表している場合、このメソッドは常にを返し null
ます。
こちらもご覧ください
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
適用対象
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
パラメーター
- binder
- Binder
一連のプロパティを定義し、バインディングを有効にするオブジェクト。バインディングには、オーバーロードされたメソッドの選択、引数の型の強制変換、リフレクションによるメンバーの呼び出しなどが含まれます。
または
Nothing
を使用する場合は、null 参照 (Visual Basic の場合は DefaultBinder)。
- types
- Type[]
取得するコンストラクターのパラメーターの数、順序、および型を表す Type オブジェクトの配列。
または パラメーターをとらないコンストラクターを取得するための、Type 型の空の配列 (Type[] types = new Type[0])。
または EmptyTypes.
- modifiers
- ParameterModifier[]
パラメーター型配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 既定のバインダーでは、このパラメーターは処理されません。
戻り値
指定した要件と一致するコンストラクターが存在する場合は、そのコンストラクターを表す ConstructorInfo オブジェクト。それ以外の場合は null
。
実装
- 属性
例外
例
次の例では、 の型を取得し 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 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 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 MyClass1 that is public " +
"and takes an integer as a parameter is:");
Console.WriteLine(constructorInfoObj.ToString());
}
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: " + 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);
}
}
}
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 指定 BindingFlags.Static Or
BindingFlags.NonPublic する必要Visual Basic。 プロパティを使用してクラス初期化子を取得 TypeInitializer できます。
注意
コンストラクターとメソッドを検索するときにパラメーターを省略することはできません。 パラメーターを省略できるのは、 を呼び出す場合のみです。
現在の が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ Type ConstructorInfo を返します。 現在の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドは常に Type を返します null
。
こちらもご覧ください
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
適用対象
GetConstructor(Type[])
指定した配列の型に一致するパラメーターが設定されているパブリック インスタンス コンストラクターを検索します。
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 によって提供されます。
戻り値
パラメーター型配列の型と一致するパラメーターが設定されているパブリック インスタンス コンストラクターが存在する場合は、そのコンストラクターを表すオブジェクト。それ以外の場合は 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;
using System.Security;
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);
}
}
}
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 を指定 BindingFlags.Static Or
BindingFlags.NonPublic Visual Basic。 プロパティを使用してクラス初期化子を取得 TypeInitializer できます。
要求されたコンストラクターが非パブリックの場合、このメソッドは を返します null
。
注意
コンストラクターとメソッドを検索するときにパラメーターを省略することはできません。 パラメーターを省略できるのは、 を呼び出す場合のみです。
現在の が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ Type ConstructorInfo を返します。 現在の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドは常に Type を返します null
。
こちらもご覧ください
- ConstructorInfo
- DefaultBinder
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
適用対象
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
。