Type.GetConstructor メソッド (BindingFlags, Binder, Type[], ParameterModifier )
指定したバインディング制約を使用して、指定した引数の型および修飾子と一致するパラメータが設定されているコンストラクタを検索します。
Overloads Public Function GetConstructor( _
ByVal bindingAttr As BindingFlags, _ ByVal binder As Binder, _ ByVal types() As Type, _ ByVal modifiers() As ParameterModifier _) As ConstructorInfo
[C#]
public ConstructorInfo GetConstructor(BindingFlagsbindingAttr,Binderbinder,Type[] types,ParameterModifier[] modifiers);
[C++]
public: ConstructorInfo* GetConstructor(BindingFlagsbindingAttr,Binder* binder,Type* types[],ParameterModifiermodifiers[]);
[JScript]
public function GetConstructor(
bindingAttr : BindingFlags,binder : Binder,types : Type[],modifiers : ParameterModifier[]) : ConstructorInfo;
パラメータ
bindingAttr
検索の実行方法を指定する 1 つ以上の BindingFlags から成るビット マスク。または
null 参照 (Visual Basic では Nothing) を返す 0。
binder
一連のプロパティを定義し、バインディングを有効にする Binder オブジェクト。バインディングには、オーバーロードされたメソッドの選択、引数の型の強制変換、リフレクションによるメンバの呼び出しなどが含まれます。または
DefaultBinder を使用する場合は null 参照 (Visual Basic では Nothing) 。
types
取得するコンストラクタのパラメータの数、順序、および型を表す Type オブジェクトの配列。または
パラメータをとらないコンストラクタを取得するための、 Type 型の空の配列 (Type[] types = new Type[0])。
または
modifiers
パラメータ型配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。既定のバインダは、このパラメータを処理しません。
戻り値
指定した要件と一致するコンストラクタが存在する場合は、そのコンストラクタを表す ConstructorInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing) 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | types が null 参照 (Visual Basic では Nothing) です。
または types の 1 つの要素が null 参照 (Nothing) です。 |
ArgumentException | types が多次元です。
または modifiers が多次元です。 または types と modifiers の長さが異なります。 |
解説
types 配列と modifiers 配列の長さが同じです。types 配列で指定するパラメータには、 modifiers 配列で指定されている pdIn、pdOut、pdLcid、pdRetval、pdOptional、pdHasDefault の各属性を設定できます。これらの属性は、それぞれ [In]、[Out]、[lcid]、[retval]、[optional]、およびパラメータが既定値を持つかどうかを指定する値を表します。パラメータに関連付けられた属性はメタデータに格納され、相互運用性を拡張します。
厳密に一致する対象が存在しない場合は、一致するものを選択するために、binder は types 配列で指定されたパラメータの型を強制的に変換しようとします。binder が一致するものを選択できない場合は、 null 参照 (Visual Basic では Nothing) が返されます。
要求されたコンストラクタがパブリックではなく、呼び出し元に現在のアセンブリ外の非パブリック メソッドをリフレクションするための ReflectionPermission がない場合、このメソッドは null 参照 (Nothing) を返します。
次の BindingFlags フィルタ フラグは、検索対象に含めるコンストラクタを定義するために使用できます。
- 戻り値を取得するには、 BindingFlags.Instance または BindingFlags.Static のいずれかを指定する必要があります。
- 検索対象にパブリック コンストラクタを含めるための BindingFlags.Public を指定します。
- 検索対象にパブリックではないコンストラクタ (つまり、プライベート コンストラクタやプロテクト コンストラクタ) を含めるための BindingFlags.NonPublic を指定します。
詳細については、「 System.Reflection.BindingFlags 」を参照してください。 GetConstructor は、クラス初期化子を取得するためには使用できません。クラス初期化子は、 GetMember 、 GetMembers 、 FindMembers 、 GetConstructors 、および TypeInitializer を通じて使用できます。
メモ コンストラクタおよびメソッドを検索する場合、パラメータは省略できません。パラメータは呼び出すときだけ省略できます。
使用例
[Visual Basic, C#, C++] MyClass1 クラスの型を取得し、指定したバインディング フラグに一致する ConstructorInfo オブジェクトを取得して、そのコンストラクタのシグネチャを表示するプログラムを次に示します。
Imports System
Imports System.Reflection
Imports System.Security
Public Class MyClass1
Public Sub New(ByVal i As Integer)
End Sub 'New
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
[C#]
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);
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public __gc class MyClass1 {
public:
MyClass1(int i) {}
};
int main() {
try {
Type* myType = __typeof(MyClass1);
Type* types[] = new Type*[1];
types->Item[0] = __typeof(int);
// Get the constructor that is public and takes an integer parameter.
ConstructorInfo* constructorInfoObj = myType->GetConstructor(static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), 0, types, 0);
if (constructorInfoObj != 0) {
Console::WriteLine(S"The constructor of MyClass1 that is public and takes an integer as a parameter is:");
Console::WriteLine(constructorInfoObj);
} else {
Console::WriteLine(S"The constructor of the MyClass1 that is public and takes an integer as a parameter is not available.");
}
} catch (ArgumentNullException* e) {
Console::WriteLine(S"ArgumentNullException: {0}", e->Message);
} catch (ArgumentException* e) {
Console::WriteLine(S"ArgumentException: {0}", e->Message);
} catch (SecurityException* e) {
Console::WriteLine(S"SecurityException: {0}", e->Message);
} catch (Exception* e) {
Console::WriteLine(S"Exception: {0}", e->Message);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
.NET Framework セキュリティ:
- ReflectionPermission (非パブリック メソッドをリフレクション操作するために必要なアクセス許可) ReflectionPermissionFlag.TypeInformation (関連する列挙体)
参照
Type クラス | Type メンバ | System 名前空間 | Type.GetConstructor オーバーロードの一覧 | ConstructorInfo | BindingFlags | Binder | DefaultBinder | ParameterModifier | ReflectionPermission | GetConstructorImpl | GetConstructors