次の方法で共有


Type.GetConstructor メソッド

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

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function GetConstructor(Type()) As ConstructorInfo

[C#] public ConstructorInfo GetConstructor(Type[]);

[C++] public: ConstructorInfo* GetConstructor(Type*[]);

[JScript] public function GetConstructor(Type[]) : ConstructorInfo;

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function GetConstructor(BindingFlags, Binder, Type(), ParameterModifier()) As ConstructorInfo

[C#] public ConstructorInfo GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]);

[C++] public: ConstructorInfo* GetConstructor(BindingFlags, Binder*, Type[], ParameterModifier[]);

[JScript] public function GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) : ConstructorInfo;

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

[Visual Basic] Overloads Public Function GetConstructor(BindingFlags, Binder, CallingConventions, Type(), ParameterModifier()) As ConstructorInfo

[C#] public ConstructorInfo GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]);

[C++] public: ConstructorInfo* GetConstructor(BindingFlags, Binder*, CallingConventions, Type[], ParameterModifier[]);

[JScript] public function GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) : ConstructorInfo;

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、GetConstructor のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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

[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 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);
        }
    }
}

[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 public instance constructor that takes an integer parameter.
      ConstructorInfo*  constructorInfoObj = myType->GetConstructor(static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), 0,
         CallingConventions::HasThis, types, 0);
      if (constructorInfoObj != 0) {
         Console::WriteLine(S"The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: ");
         Console::WriteLine(constructorInfoObj);
      } else {
         Console::WriteLine(S"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(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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Type クラス | Type メンバ | System 名前空間