Type.GetConstructorとは? わかりやすく解説

Type.GetConstructor メソッド (BindingFlags, Binder, Type[], ParameterModifier[])

指定したバインディング制約を使用して、指定した引数の型および修飾子と一致するパラメータが設定されているコンストラクタを検索します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Function GetConstructor ( _
    bindingAttr As BindingFlags, _
    binder As Binder, _
    types As Type(), _
    modifiers As ParameterModifier() _
) As ConstructorInfo
Dim instance As Type
Dim bindingAttr As BindingFlags
Dim binder As Binder
Dim types As Type()
Dim modifiers As ParameterModifier()
Dim returnValue As ConstructorInfo

returnValue = instance.GetConstructor(bindingAttr, binder, types, modifiers)
[ComVisibleAttribute(true)] 
public ConstructorInfo GetConstructor (
    BindingFlags bindingAttr,
    Binder binder,
    Type[] types,
    ParameterModifier[] modifiers
)
[ComVisibleAttribute(true)] 
public:
virtual ConstructorInfo^ GetConstructor (
    BindingFlags bindingAttr, 
    Binder^ binder, 
    array<Type^>^ types, 
    array<ParameterModifier>^ modifiers
) sealed
/** @attribute ComVisibleAttribute(true) */ 
public final ConstructorInfo GetConstructor (
    BindingFlags bindingAttr, 
    Binder binder, 
    Type[] types, 
    ParameterModifier[] modifiers
)
ComVisibleAttribute(true) 
public final 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])。

または

EmptyTypes.

modifiers

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

戻り値
指定した要件と一致するコンストラクタが存在する場合は、そのコンストラクタを表す ConstructorInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

types が null 参照 (Visual Basic では Nothing) です。

または

types の 1 つの要素が null 参照 (Visual Basic では 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 参照 (Visual Basic では Nothing) を返します。

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

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

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

メモメモ

コンストラクタおよびメソッドを検索する場合、パラメータは省略できません。パラメータは呼び出すときだけ省略できます。

現在の Type が構築ジェネリック型を表している場合、このメソッドは、型パラメータを適切な型の引数で置き換えて ConstructorInfo を返します。現在の Type がジェネリック型またはジェネリック メソッドの定義の型パラメータを表している場合、このメソッドは常に null 参照 (Visual Basic では Nothing) を返します。

使用例使用例

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
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);
        }
    }
}
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 );
   }
}
import System.*;
import System.Reflection.*;
import System.Security.*;

public class MyClass1
{
    public MyClass1(int i)
    {
    } //MyClass1

    public static void main(String[]
 args)
    {
        try {
            Type myType = MyClass1.class.ToType();
            Type types[] = new Type[1];
            types.set_Item(0, int.class.ToType());
            // 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.get_Message());
        }
        catch (ArgumentException e) {
            Console.WriteLine("ArgumentException: " + e.get_Message());
        }
        catch (SecurityException e) {
            Console.WriteLine("SecurityException: " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: " + e.get_Message());
        }
    } //main
} //MyClass1
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
ConstructorInfo
BindingFlags
Binder
DefaultBinder
ParameterModifier
ReflectionPermission
GetConstructorImpl
GetConstructors

Type.GetConstructor メソッド (BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Function GetConstructor ( _
    bindingAttr As BindingFlags, _
    binder As Binder, _
    callConvention As CallingConventions, _
    types As Type(), _
    modifiers As ParameterModifier() _
) As ConstructorInfo
Dim instance As Type
Dim bindingAttr As BindingFlags
Dim binder As Binder
Dim callConvention As CallingConventions
Dim types As Type()
Dim modifiers As ParameterModifier()
Dim returnValue As ConstructorInfo

returnValue = instance.GetConstructor(bindingAttr, binder, callConvention, types,
 modifiers)
[ComVisibleAttribute(true)] 
public ConstructorInfo GetConstructor (
    BindingFlags bindingAttr,
    Binder binder,
    CallingConventions callConvention,
    Type[] types,
    ParameterModifier[] modifiers
)
[ComVisibleAttribute(true)] 
public:
virtual ConstructorInfo^ GetConstructor (
    BindingFlags bindingAttr, 
    Binder^ binder, 
    CallingConventions callConvention, 
    array<Type^>^ types, 
    array<ParameterModifier>^ modifiers
) sealed
/** @attribute ComVisibleAttribute(true) */ 
public final ConstructorInfo GetConstructor (
    BindingFlags bindingAttr, 
    Binder binder, 
    CallingConventions callConvention, 
    Type[] types, 
    ParameterModifier[] modifiers
)
ComVisibleAttribute(true) 
public final function GetConstructor (
    bindingAttr : BindingFlags, 
    binder : Binder, 
    callConvention : CallingConventions, 
    types : Type[], 
    modifiers : ParameterModifier[]
) : ConstructorInfo

パラメータ

bindingAttr

検索の実行方法を指定する 1 つ以上の BindingFlags から成るビット マスク。

または

null 参照 (Visual Basic では Nothing) を返す 0。

binder

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

または

DefaultBinder を使用する場合は null 参照 (Visual Basic では Nothing)。

callConvention

引数の順序とレイアウト、戻り値を渡す方法、引数を格納するレジスタ、スタックのクリーンアップに関する一連の規則を指定する CallingConventions オブジェクト。

types

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

または

パラメータをとらないコンストラクタを取得するための、Type 型の空の配列 (Type[] types = new Type[0])。

modifiers

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

戻り値
指定した要件と一致するコンストラクタが存在する場合は、そのコンストラクタを表す ConstructorInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

types が null 参照 (Visual Basic では Nothing) です。

または

types の 1 つの要素が null 参照 (Visual Basic では Nothing) です。

ArgumentException

types が多次元です。

または

modifiers が多次元です。

または

types と modifiers の長さが異なります。

解説解説

既定のバインダは ParameterModifier (modifiers パラメータ) を処理しませんが、System.Reflection.Binder 抽象クラスを使用してmodifiers を処理するカスタム バインダを記述できます。ParameterModifier は、COM 相互運用機能によって呼び出すときだけに使用され、参照渡しされるパラメータだけが処理されます。

types 配列と modifiers 配列の長さが同じです。types 配列で指定するパラメータには、modifiers 配列で指定されている pdIn、pdOut、pdLcid、pdRetval、pdOptional、pdHasDefault の各属性を設定できます。これらの属性は、それぞれ [In]、[Out]、[lcid]、[retval]、[optional]、およびパラメータが既定値を持つかどうかを指定する値を表します。パラメータに関連付けられた属性はメタデータに格納され、相互運用性を拡張します。

厳密に一致する対象が存在しない場合は、一致するものを選択するために、binder は types 配列で指定されたパラメータの型を強制的に変換しようとします。binder が一致するものを選択できない場合は、null 参照 (Visual Basic では Nothing) が返されます。

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

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

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

要求されたコンストラクタがパブリックではなく、呼び出し元に現在のアセンブリ外の非パブリック メソッドをリフレクションするための ReflectionPermission がない場合、このメソッドは null 参照 (Visual Basic では Nothing) を返します。

型に対するリフレクション時に Get メソッドによって返される基本クラスのメンバを次の表に示します。

  1. 名前と署名による隠ぺいでは、カスタム修飾子、戻り値の型、パラメータの型、sentinel、およびアンマネージ呼び出し規約を含めて、署名のすべての部分が判断の対象となります。これはバイナリ比較です。

  2. リフレクションの場合、プロパティおよびイベントは名前と署名によって隠ぺいされています。基本クラスに get アクセサと set アクセサの両方を持つプロパティがあり、派生クラスには get アクセサしかない場合、派生クラスのプロパティにより基本クラスのプロパティが隠ぺいされ、基本クラスの set アクセサにはアクセスできません。

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

現在の Type が構築ジェネリック型を表している場合、このメソッドは、型パラメータを適切な型の引数で置き換えて ConstructorInfo を返します。現在の Type がジェネリック型またはジェネリック メソッドの定義の型パラメータを表している場合、このメソッドは常に null 参照 (Visual Basic では Nothing) を返します。

使用例使用例

MyClass1 の型を取得し、指定したバインディング フラグと一致する ConstructorInfo オブジェクトを取得して、コンストラクタのシグネチャを表示する例を次に示します。

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
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);
        }
    }
}
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 );
   }
}
import System.*;
import System.Reflection.*;
import System.Security.*;

public class MyClass1
{
    public MyClass1(int i)
    {
    } //MyClass1

    public static void main(String[]
 args)
    {
        try {
            Type myType = MyClass1.class.ToType();
            Type types[] = new Type[1];
            types.set_Item(0, int.class.ToType());
            // 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.get_Message());
        }
        catch (ArgumentException e) {
            Console.WriteLine("ArgumentException: " + e.get_Message());
        }
        catch (SecurityException e) {
            Console.WriteLine("SecurityException: " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: " + e.get_Message());
        }
    } //main
} //MyClass1
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
ConstructorInfo
BindingFlags
Binder
DefaultBinder
CallingConventions
ParameterModifier
ReflectionPermission
GetConstructorImpl
GetConstructors

Type.GetConstructor メソッド (Type[])

指定した配列の型に一致するパラメータが設定されているパブリック インスタンス コンストラクタを検索します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Function GetConstructor ( _
    types As Type() _
) As ConstructorInfo
Dim instance As Type
Dim types As Type()
Dim returnValue As ConstructorInfo

returnValue = instance.GetConstructor(types)
[ComVisibleAttribute(true)] 
public ConstructorInfo GetConstructor (
    Type[] types
)
[ComVisibleAttribute(true)] 
public:
virtual ConstructorInfo^ GetConstructor (
    array<Type^>^ types
) sealed
/** @attribute ComVisibleAttribute(true) */ 
public final ConstructorInfo GetConstructor (
    Type[] types
)
ComVisibleAttribute(true) 
public final function GetConstructor (
    types : Type[]
) : ConstructorInfo

パラメータ

types

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

または

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

戻り値
パラメータ型配列の型と一致するパラメータが設定されているパブリック インスタンス コンストラクタが存在する場合は、そのコンストラクタを表す ConstructorInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

types が null 参照 (Visual Basic では Nothing) です。

または

types の 1 つの要素が null 参照 (Visual Basic では Nothing) です。

ArgumentException

types が多次元です。

解説解説
使用例使用例

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

Imports System
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

Public Class MyClass1

    Public Sub New()
    End Sub 'New

    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(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 'Main
End Class 'MyClass1
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);
        }
    }
}
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 );
   }
}
import System.*;
import System.Reflection.*;
import System.Security.*;

public class MyClass1
{
    public MyClass1()
    {
    } //MyClass1

    public MyClass1(int i)
    {
    } //MyClass1

    public static void main(String[]
 args)
    {
        try {
            Type myType = MyClass1.class.ToType();
            Type types[] = new Type[1];
            types.set_Item(0, int.class.ToType());
            // 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 (System.Exception e) {
            Console.WriteLine("Exception caught.");
            Console.WriteLine("Source: " + e.get_Source());
            Console.WriteLine("Message: " + e.get_Message());
        }
    } //main
} //MyClass1
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
ConstructorInfo
DefaultBinder
ReflectionPermission
GetConstructorImpl
GetConstructors

Type.GetConstructor メソッド


_Type.GetConstructor メソッド

COM オブジェクトに、System.Type.GetConstructor メソッドへのバージョンに依存しないアクセスが用意されています。
オーバーロードの一覧オーバーロードの一覧

名前 説明
_Type.GetConstructor (Type[]) COM オブジェクトに、Type.GetConstructor メソッドへのバージョンに依存しないアクセスが用意されています。
_Type.GetConstructor (BindingFlags, Binder, Type[], ParameterModifier[]) COM オブジェクトに、Type.GetConstructor メソッドへのバージョンに依存しないアクセスが用意されています。
_Type.GetConstructor (BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) COM オブジェクトに、Type.GetConstructor メソッドへのバージョンに依存しないアクセスが用意されています。
参照参照

関連項目

_Type インターフェイス
_Type メンバ
System.Runtime.InteropServices 名前空間



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Type.GetConstructor」の関連用語

Type.GetConstructorのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Type.GetConstructorのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS