Type.GetMethod メソッド

定義

現在の Type の特定のメソッドを取得します。

オーバーロード

GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

指定したバインディング制約および指定した呼び出し規約を使用して、指定したメソッドのうち、指定したジェネリック パラメーターの数、引数の型、および修飾子と一致するパラメーターが設定されているものを検索します。

GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

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

GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])

指定したバインディング制約を使用して、指定したメソッドのうち、指定したジェネリック パラメーターの数、引数の型、および修飾子と一致するパラメーターが設定されているものを検索します。

GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

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

GetMethod(String, Int32, Type[], ParameterModifier[])

指定したジェネリック パラメーターの数、引数の型、および修飾子に一致するパラメーターを持つ、指定したパブリック メソッドを検索します。

GetMethod(String, Int32, Type[])

指定したジェネリック パラメーターの数と引数の型に一致するパラメーターを持つ、指定したパブリック メソッドを検索します。

GetMethod(String, BindingFlags, Type[])

指定したバインディング制約を使用して、指定した引数の型に一致するパラメーターを持つ指定したメソッドを検索します。

GetMethod(String, Type[])

指定したパブリック メソッドのうち、指定した引数型と一致するパラメーターが設定されているものを検索します。

GetMethod(String, BindingFlags)

指定したバインディング制約を使用して、指定したメソッドを検索します。

GetMethod(String)

指定した名前のパブリック メソッドを検索します。

GetMethod(String, Type[], ParameterModifier[])

指定したパブリック メソッドのうち、指定した引数の型および修飾子と一致するパラメーターが設定されているものを検索します。

GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

指定したバインディング制約および指定した呼び出し規約を使用して、指定したメソッドのうち、指定したジェネリック パラメーターの数、引数の型、および修飾子と一致するパラメーターが設定されているものを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, 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.MethodInfo? GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo

パラメーター

name
String

取得するパブリック メソッドの名前を格納している文字列。

genericParameterCount
Int32

メソッドのジェネリック型パラメーターの数。

bindingAttr
BindingFlags

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

または

null を返す場合は Default

binder
Binder

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

または

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

callConvention
CallingConventions

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

types
Type[]

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

または

パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

modifiers
ParameterModifier[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 COM 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

MethodInfo

指定したジェネリック パラメーターの数、引数の型、修飾子、バインディングの制約、および呼び出し規約に一致するメソッドが存在する場合は、それを表すオブジェクト。それ以外の場合は null

例外

namenullです。

または

typesnullです。

または

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

genericParameterCount が負の値です。

適用対象

GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

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

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, 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::MethodInfo ^ GetMethod(System::String ^ name, 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.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo

パラメーター

name
String

取得するメソッドの名前を格納している文字列。

bindingAttr
BindingFlags

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

または null を返す場合は Default

binder
Binder

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

または Nothing を使用する場合は、null 参照 (Visual Basic の場合は DefaultBinder)。

callConvention
CallingConventions

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

types
Type[]

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

または パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

modifiers
ParameterModifier[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 COM 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

MethodInfo

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は null

実装

例外

指定した名前を持ち、指定されたバインド制約に一致する 2 つ以上のメソッドが存在します。

namenullです。

または typesnullです。

または types の要素の 1 つが null です。

types が多次元です。

または modifiers が多次元です。

次の例では MethodA 、バインディング制約、呼び出し規約、およびさまざまな引数型を指定して、の特定のオーバーロードを検索します。

注意

Visual C# 2005 の例では、コンパイラオプションが必要です /unsafe

using namespace System;
using namespace System::Reflection;

public ref class Program
{

public:
    // Methods to get:

    void MethodA(int i, int j) { }

    void MethodA(array<int>^ iarry) { }

    void MethodA(double *ip) { }

    // Method that takes a managed reference paramter.
    void MethodA(int% r) {}
};

int main()
{
    MethodInfo^ mInfo;


    // Get MethodA(int i, int j)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {int::typeid, int::typeid},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get  MethodA(array<int>^ iarry)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {int::typeid->MakeArrayType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(double *ip)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {double::typeid->MakePointerType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(int% r)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {int::typeid->MakeByRefType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

}

using System;
using System.Reflection;

class Program3
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter:
    public void MethodA(int i, out int o) { o = 100;}

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);
  }
}

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:
    
    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an integer and an out parameter. Note that an
    ' Imports reference is needed to System.Runtime.InteropServices
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(ByVal i As Integer, ByVal l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            CallingConventions.Any, _
            New Type() {GetType(System.Int32), _
            GetType(System.Int64)}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get  MethodA(ByVal i() As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            CallingConventions.Any, _
            New Type() {GetType(System.Int32())}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
        BindingFlags.Public Or BindingFlags.Instance, _
        Nothing, _
        CallingConventions.Any, _
        New Type() {GetType(System.Int32).MakeByRefType}, _
        Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
        BindingFlags.Public Or BindingFlags.Instance, _
        Nothing, _
        CallingConventions.Any, _
        New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
        Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注釈

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

次の表は、型に対してリフレクションを行うときに、メソッドによって返される基底クラスのメンバーを示してい GetXXX ます。

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

  2. リフレクションの場合、プロパティとイベントは、名前とシグネチャが隠ぺいされます。 基底クラスに get と set の両方のアクセサーを持つプロパティがあり、派生クラスに get アクセサーのみがある場合、派生クラスのプロパティは基底クラスのプロパティを非表示にします。基底クラスの setter にアクセスすることはできません。

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

次の BindingFlags フィルターフラグを使用して、検索に含めるメソッドを定義できます。

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

  • 検索にパブリックメソッドを含める場合は、を指定し BindingFlags.Public ます。

  • BindingFlags.NonPublic検索に非パブリックメソッド (プライベート、内部、および保護されたメソッド) を含めるように指定します。

  • BindingFlags.FlattenHierarchy階層の上位にとの静的メンバーを含めるように指定します。 public protected 継承された private クラスの静的メンバーは含まれません。

次の BindingFlags 修飾子フラグを使用して、検索の動作を変更できます。

  • BindingFlags.IgnoreCase の大文字と小文字を区別しない場合は name

  • BindingFlags.DeclaredOnly で宣言されたメソッドだけを検索する Type 場合は。単純に継承されたメソッドではない場合。

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

注意

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

現在のが Type 構築ジェネリック型を表している場合、このメソッドは、 MethodInfo 適切な型引数によって置き換えられた型パラメーターを持つを返します。

現在のが Type ジェネリック型またはジェネリックメソッドの定義の型パラメーターを表している場合、このメソッドはクラスの制約のメソッド、または Object クラスの制約がない場合はのメソッドを検索します。

注意

ジェネリックメソッドの場合は、型引数をに含めないで name ください。 たとえば、C# コードは、 GetMember("MyMethod<int>") MyMethod<int> MyMethod 型のジェネリック引数を1つ持つという名前のメソッドではなく、テキスト名 "" を持つメンバーを検索し int ます。

こちらもご覧ください

適用対象

GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])

指定したバインディング制約を使用して、指定したメソッドのうち、指定したジェネリック パラメーターの数、引数の型、および修飾子と一致するパラメーターが設定されているものを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo

パラメーター

name
String

取得するパブリック メソッドの名前を格納している文字列。

genericParameterCount
Int32

メソッドのジェネリック型パラメーターの数。

bindingAttr
BindingFlags

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

または

null を返す場合は Default

binder
Binder

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

または

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

types
Type[]

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

または

パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

modifiers
ParameterModifier[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 COM 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

MethodInfo

指定したジェネリック パラメーターの数、引数の型、修飾子、およびバインディングの制約に一致するメソッドが存在する場合は、それを表すオブジェクト。それ以外の場合は null

例外

namenullです。

または

typesnullです。

または

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

genericParameterCount が負の値です。

適用対象

GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

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

public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo

パラメーター

name
String

取得するメソッドの名前を格納している文字列。

bindingAttr
BindingFlags

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

または null を返す場合は Default

binder
Binder

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

または Nothing を使用する場合は、null 参照 (Visual Basic の場合は DefaultBinder)。

types
Type[]

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

または パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

modifiers
ParameterModifier[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 COM 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

MethodInfo

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は null

実装

例外

指定した名前を持ち、指定されたバインド制約に一致する 2 つ以上のメソッドが存在します。

namenullです。

または typesnullです。

または types の要素の 1 つが null です。

types が多次元です。

または modifiers が多次元です。

次の例では、 の特定のオーバーロードを検索し MethodA 、バインド制約とさまざまな引数の型を指定します。

注意

Visual C# 2005 の例では、コンパイラ オプションが /unsafe 必要です。

using namespace System;
using namespace System::Reflection;

public ref class Program
{

public:
    // Methods to get:

    void MethodA(int i, int j) { }

    void MethodA(array<int>^ iarry) { }

    void MethodA(double *ip) { }

    // Method that takes a managed reference parameter.
    void MethodA(int% r) {}
};

int main()
{
    MethodInfo^ mInfo;


    // Get MethodA(int i, int j)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {int::typeid, int::typeid},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get  MethodA(array<int>^ iarry)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {int::typeid->MakeArrayType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(double *ip)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {double::typeid->MakePointerType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(int% r)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {int::typeid->MakeByRefType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );
}

using System;
using System.Reflection;

class Program5
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter.
    public void MethodA(int i, out int o) { o = 100; }

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);
  }
}

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:
    
    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an out parameter. Note that an Imports
    ' reference is needed to System.Runtime.InteropServices 
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(ByVal i As Integer, ByVal l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32), _
            GetType(System.Int64)}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i() As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32())}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32).MakeByRefType}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注釈

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

検索に BindingFlags 含めるメソッドを定義するには、次のフィルター フラグを使用できます。

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

  • 検索 BindingFlags.Public にパブリック メソッドを含めるには、 を指定します。

  • 非パブリック メソッド (つまり、プライベート、内部、保護されたメソッド) を検索に含 BindingFlags.NonPublic める場合に指定します。

  • 階層 BindingFlags.FlattenHierarchy のメンバーと静的メンバーを含める場合は を指定します。継承されたクラスの静的 public protected private メンバーは含まれません。

検索のしくみ BindingFlags を変更するには、次の修飾子フラグを使用できます。

  • BindingFlags.IgnoreCase の大文字と小文字を無視する場合 name は 。

  • BindingFlags.DeclaredOnly 単純に継承されたメソッドではなく、 で宣言されたメソッド Type のみを検索する場合は 。

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

注意

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

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

現在の がジェネリック型またはジェネリック メソッドの定義の型パラメーターを表している場合、このメソッドはクラス制約のメソッドを検索します。クラス制約がない場合は、 のメソッドを検索します。 Type Object

注意

ジェネリック メソッドの場合は、 に型引数を含め "しない" 必要があります name 。 たとえば、C# コードでは、 型のジェネリック引数を 1 つ持つ という名前のメソッドではなく、"" というテキスト名のメンバー GetMember("MyMethod<int>") MyMethod<int> MyMethod を検索します int

こちらもご覧ください

適用対象

GetMethod(String, Int32, Type[], ParameterModifier[])

指定したジェネリック パラメーターの数、引数の型、および修飾子に一致するパラメーターを持つ、指定したパブリック メソッドを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * int * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type(), modifiers As ParameterModifier()) As MethodInfo

パラメーター

name
String

取得するパブリック メソッドの名前を格納している文字列。

genericParameterCount
Int32

メソッドのジェネリック型パラメーターの数。

types
Type[]

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

または

パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

modifiers
ParameterModifier[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 COM 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

MethodInfo

指定したジェネリック パラメーターの数、引数の型、および修飾子に一致するパブリック メソッドが存在する場合は、それを表すオブジェクト。それ以外の場合は null

例外

namenullです。

または

typesnullです。

または

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

genericParameterCount が負の値です。

適用対象

GetMethod(String, Int32, Type[])

指定したジェネリック パラメーターの数と引数の型に一致するパラメーターを持つ、指定したパブリック メソッドを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, Type[] types);
member this.GetMethod : string * int * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type()) As MethodInfo

パラメーター

name
String

取得するパブリック メソッドの名前を格納している文字列。

genericParameterCount
Int32

メソッドのジェネリック型パラメーターの数。

types
Type[]

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

または

パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

戻り値

MethodInfo

指定したジェネリック パラメーターの数と引数の型に一致するパラメーターを持つパブリック メソッドが存在する場合は、それを表すオブジェクト。それ以外の場合は null

例外

namenullです。

または

typesnullです。

または

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

genericParameterCount が負の値です。

適用対象

GetMethod(String, BindingFlags, Type[])

指定したバインディング制約を使用して、指定した引数の型に一致するパラメーターを持つ指定したメソッドを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr, Type[] types);
member this.GetMethod : string * System.Reflection.BindingFlags * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, types As Type()) As MethodInfo

パラメーター

name
String

取得するメソッドの名前を格納している文字列。

bindingAttr
BindingFlags

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

types
Type[]

取得するメソッドのパラメーターの数、順序、および型を表す Type オブジェクトの配列。 または、パラメーターを受け取らないメソッドを取得するオブジェクトの空の配列 (フィールドによって提供 Type EmptyTypes されます)。

戻り値

MethodInfo

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は null

適用対象

GetMethod(String, Type[])

指定したパブリック メソッドのうち、指定した引数型と一致するパラメーターが設定されているものを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, Type[] types);
public System.Reflection.MethodInfo GetMethod (string name, Type[] types);
member this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type()) As MethodInfo

パラメーター

name
String

取得するパブリック メソッドの名前を格納している文字列。

types
Type[]

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

または パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

戻り値

MethodInfo

指定した引数型と一致するパラメーターが設定されたパブリック メソッドが存在する場合は、そのパブリック メソッドを表すオブジェクト。それ以外の場合は null

実装

例外

指定された名前および指定されたパラメーターを持つメソッドが複数見つかりました。

namenullです。

または typesnullです。

または types の要素の 1 つが null です。

types が多次元です。

次の例では、 のさまざまな引数型を指定 MethodA する の特定のオーバーロードを検索します。

注意

Visual C# 2005 の例では、コンパイラ オプションが /unsafe 必要です。

using namespace System;
using namespace System::Reflection;

public ref class Program
{

public:
    // Methods to get:

    void MethodA(int i, int j) { }

    void MethodA(array<int>^ iarry) { }

    void MethodA(double *ip) { }

    // Method that takes a managed reference parameter.
    void MethodA(int% r) {}
};

int main()
{
    MethodInfo^ mInfo;


    // Get MethodA(int i, int j)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid,int::typeid});
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(array<int>^ iarry)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid->MakeArrayType()});
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(double *ip)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {double::typeid->MakePointerType()});
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(int% r)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid->MakeByRefType()});
    // Display the method information.
    Console::WriteLine("Found method: {0}", mInfo );

}

using System;
using System.Reflection;

class Program4
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter:
    public void MethodA(int i, out int o) { o = 100;}

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int[]) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int).MakePointerType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);
  }
}

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:

    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an out parameter. Note that an Imports
    ' reference is needed to System.Runtime.InteropServices 
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(i As Integer i, l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), GetType(Long)})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get  MethodA(i As Integer())
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer())})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer).MakeByRefType})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(i As Integer, ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), _
            GetType(Integer).MakeByRefType})
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

次の例では、非ジェネリック型 (クラス)、オープン ジェネリック型 (クラス)、および閉じたジェネリック型 (型) のメソッドを表すオブジェクトを取得 MethodInfo Add ArrayList List<T> List(Of String) します。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Get a Type object that represents a non-generic type.
      GetAddMethod(typeof(ArrayList));

      var list = new List<String>();
      // Get a Type object that represents a constructed generic type.
      Type closed = list.GetType();
      GetAddMethod(closed);
      
      // Get a Type object that represents an open generic type.
      Type open = typeof(List<>);
      GetAddMethod(open);
   }

   private static void GetAddMethod(Type typ)
   {
      MethodInfo method;
      // Determine if this is a generic type.
      if (typ.IsGenericType) {
         // Is it an open generic type?
         if (typ.ContainsGenericParameters)
            method = typ.GetMethod("Add", typ.GetGenericArguments());
         // Get closed generic type arguments.
         else
            method = typ.GetMethod("Add", typ.GenericTypeArguments);
      }
      // This is not a generic type.
      else {
         method = typ.GetMethod("Add", new Type[] { typeof(Object) } );
      }

      // Test if an Add method was found.
      if (method == null) { 
         Console.WriteLine("No Add method found.");
         return;
      }   
      
      Type t = method.ReflectedType;
      Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name);
      ParameterInfo[] parms = method.GetParameters();
      for (int ctr = 0; ctr < parms.Length; ctr++)
         Console.Write("{0}{1}", parms[ctr].ParameterType.Name, 
                       ctr < parms.Length - 1 ? ", " : "");

      Console.WriteLine(")");
   }   
}
// The example displays the following output:
//       System.Collections.ArrayList.Add(Object)
//       System.Collections.Generic.List`1.Add(String)
//       System.Collections.Generic.List`1.Add(T)
Imports System.Collections
Imports System.Collections.Generic
Imports System.Reflection

Module Example
   Public Sub Main()
      ' Get a Type object that represents a non-generic type.
      GetAddMethod(GetType(ArrayList))
      
      Dim list As New List(Of String)()
      ' Get a Type object that represents a constructed generic type.
      Dim closed As Type = list.GetType()
      GetAddMethod(closed)
      
      ' Get a Type object that represents an open generic type.
      Dim open As Type = GetType(List(Of))
      GetAddMethod(open)
   End Sub
   
   Private Sub GetAddMethod(typ As Type)
      Dim method As MethodInfo
      ' Determine if this is a generic type.
      If typ.IsGenericType Then
         ' Is it an open generic type?
         If typ.ContainsGenericParameters Then
            method = typ.GetMethod("Add", typ.GetGenericArguments())
         ' Get closed generic type arguments.
         Else
            method = typ.GetMethod("Add", typ.GenericTypeArguments)
         End If
      ' This is not a generic type.
      Else
         method = typ.GetMethod("Add", { GetType(Object) } )
      End If
      ' Test if an Add method was found.
      If method Is Nothing Then 
         Console.WriteLine("No Add method found.")
         Exit Sub
      End If   

      Dim t As Type = method.ReflectedType
      Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name)
      Dim params() As ParameterInfo = method.GetParameters()
      For ctr As Integer = 0 To params.Length - 1
         Console.Write("{0}{1}", params(ctr).ParameterType.Name, 
                       If(ctr < params.Length - 1, ", ", ""))
      Next
      Console.WriteLine(")")
   End Sub
End Module
' The example displays the following output:
'       System.Collections.ArrayList.Add(Object)
'       System.Collections.Generic.List`1.Add(String)
'       System.Collections.Generic.List`1.Add(T)

この例では、適切な GetAddMethod オブジェクトを取得するメソッドを定義 MethodInfo します。 オープン ジェネリック型 types の引数を指定するには、 メソッドを呼び出 Type.GetGenericArguments します。 閉じたジェネリック types 型の引数を指定するために、 プロパティの値を取得 Type.GenericTypeArguments します。

注釈

の検索では name 、大文字と小文字が区別されます。 検索には、パブリックの静的インスタンス メソッドとパブリック インスタンス メソッドが含まれています。

注意

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

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

現在の がジェネリック型またはジェネリック メソッドの定義の型パラメーターを表している場合、このメソッドはクラス制約のメソッドを検索します。クラス制約がない場合は、 のメソッドを検索します。 Type Object

注意

パラメーター name に型引数を含めすることはできません。 たとえば、C# コードでは、 型のジェネリック引数を 1 つ持つ という名前のメソッドではなく、"" というテキスト名のメソッド GetMethod("MyGenericMethod<int>") MyGenericMethod<int> MyGenericMethod を検索します int 。 代わりに、 を GetMethod("MyGenericMethod") 配列の適切なパラメーターと一緒に使用 types します。

こちらもご覧ください

適用対象

GetMethod(String, BindingFlags)

指定したバインディング制約を使用して、指定したメソッドを検索します。

public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public System.Reflection.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.MethodInfo GetMethod (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags) As MethodInfo

パラメーター

name
String

取得するメソッドの名前を格納している文字列。

bindingAttr
BindingFlags

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

または null を返す場合は Default

戻り値

MethodInfo

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は null

実装

例外

指定した名前を持ち、指定されたバインド制約に一致する 2 つ以上のメソッドが存在します。

namenullです。

次の例では、指定したバインディング フラグに一致するメソッドを取得します。

using namespace System;
using namespace System::Reflection;
public ref class Program
{

    public:

        // Method to get:
        void MethodA() { }

    };

    int main()
    {

        // Get MethodA()
        MethodInfo^ mInfo = Program::typeid->GetMethod("MethodA",
            static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
        Console::WriteLine("Found method: {0}", mInfo );

    }

using System;
using System.Reflection;

class Program2
{

    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {

        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA",
            BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine("Found method: {0}", mInfo);
    }
}

Imports System.Reflection

Class Program

    ' Method to get:
    Public Sub MethodA()
    End Sub


    Public Shared Sub Main(ByVal args() As String)

        ' Get MethodA()
        Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注釈

検索に BindingFlags 含めるメソッドを定義するには、次のフィルター フラグを使用できます。

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

  • 検索 BindingFlags.Public にパブリック メソッドを含めるには、 を指定します。

  • 非パブリック メソッド (つまり、プライベート、内部、および保護されたメソッド) を検索に含 BindingFlags.NonPublic める場合に指定します。

  • 階層 BindingFlags.FlattenHierarchy のメンバーと静的メンバーを含める場合は を指定します。継承されたクラスの静的 public protected private メンバーは含まれません。

検索のしくみ BindingFlags を変更するには、次の修飾子フラグを使用できます。

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

メソッドがオーバーロードされ、複数のオーバーロードが 引数で指定された制約を満たす場合、メソッドは bindingAttr 例外をスロー AmbiguousMatchException します。 次の例では、次の理由で例外がスローされます。

  • TestClass には、 メソッドと の 2 つのパブリック インスタンス DisplayValue オーバーロード DisplayValue(String) があります DisplayValue(String, Object[])

  • 型には、 メソッドの 2 つのパブリック インスタンス オーバーロードがあります。そのうちの 1 つは TestClass Equals 、 と Object から継承 Equals(TestClass) されます Equals(Object)

using System;
using System.Reflection;

public class TestClass
{
   public void DisplayValue(String s)
   {
      Console.WriteLine(s);
   }
   
   public void DisplayValue(String s, params Object[] values)
   {
      Console.WriteLine(s, values);
   }
   
   public static bool Equals(TestClass t1, TestClass t2)
   {
      return Object.ReferenceEquals(t1, t2);
   }
   
   public bool Equals(TestClass t) 
   {
      return Object.ReferenceEquals(this, t);
   }          
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(TestClass);
      
      RetrieveMethod(t, "DisplayValue", BindingFlags.Public | BindingFlags.Instance);

      RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance);
      
      RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
      
      RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Static);
   }
   
   private static void RetrieveMethod(Type t, String name, BindingFlags flags)
   {
      try {
         MethodInfo m = t.GetMethod(name, flags);
         if (m != null) {
            Console.Write("{0}.{1}(", t.Name, m.Name);
            ParameterInfo[] parms= m.GetParameters();
            for (int ctr = 0; ctr <parms.Length; ctr++) {
               Console.Write(parms[ctr].ParameterType.Name);
               if (ctr < parms.Length - 1) 
                  Console.Write(", ");
            }
            Console.WriteLine(")");
         }
         else {
            Console.WriteLine("Method not found");
         }
      }
      catch (AmbiguousMatchException) {
         Console.WriteLine("The following duplicate matches were found:");
         MethodInfo[] methods = t.GetMethods(flags);
         foreach (var method in methods) {
            if (method.Name != name) continue;

            Console.Write("   {0}.{1}(", t.Name, method.Name);
            ParameterInfo[] parms = method.GetParameters();
            for (int ctr = 0; ctr < parms.Length; ctr++) {
               Console.Write(parms[ctr].ParameterType.Name);
               if (ctr < parms.Length - 1) 
                  Console.Write(", ");
            }
            Console.WriteLine(")");
         } 
      }         
      Console.WriteLine();
   }
}
// The example displays the following output:
//       The following duplicate matches were found:
//          TestClass.DisplayValue(String)
//          TestClass.DisplayValue(String, Object[])
//       
//       The following duplicate matches were found:
//          TestClass.Equals(TestClass)
//          TestClass.Equals(Object)
//       
//       TestClass.Equals(TestClass)
//       
//       TestClass.Equals(TestClass, TestClass)
Imports System.Reflection

Public Class TestClass
   Public Sub DisplayValue(s As String)
      Console.WriteLine(s)
   End Sub
   
   Public Sub DisplayValue(s As String, ParamArray values() As Object)
      Console.WriteLine(s, values)
   End Sub
   
   Public Overloads Shared Function Equals(t1 As TestClass, t2 As TestClass) As Boolean
      Return Object.ReferenceEquals(t1, t2)
   End Function
   
   Public Overloads Function Equals(t As TestClass) As Boolean
      Return Object.ReferenceEquals(Me, t)
   End Function          
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(TestClass)
      
      RetrieveMethod(t, "DisplayValue", BindingFlags.Public Or BindingFlags.Instance)

      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance)
      
      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
      
      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Static)
   End Sub
   
   Public Sub RetrieveMethod(t As Type, name As String, flags As BindingFlags)
      Try
         Dim m As MethodInfo = t.GetMethod(name, flags)
         If m IsNot Nothing Then
            Console.Write("{0}.{1}(", t.Name, m.Name)
           Dim parms() As ParameterInfo = m.GetParameters()
            For ctr As Integer = 0 To parms.Length - 1
               Console.Write(parms(ctr).ParameterType.Name)
               if ctr < parms.Length - 1 Then 
                  Console.Write(", ")
               End If      
            Next
            Console.WriteLine(")")
         Else
            Console.WriteLine("Method not found")
         End If
      Catch e As AmbiguousMatchException
         Console.WriteLine("The following duplicate matches were found:")
         Dim methods() As MethodInfo = t.GetMethods(flags)
         For Each method In methods
            If method.Name <> name Then Continue For

            Console.Write("   {0}.{1}(", t.Name, method.Name)
            Dim parms() As ParameterInfo = method.GetParameters()
            For ctr As Integer = 0 To parms.Length - 1
               Console.Write(parms(ctr).ParameterType.Name)
               if ctr < parms.Length - 1 Then 
                  Console.Write(", ")
               End If      
            Next
            Console.WriteLine(")")
         Next 
      End Try         
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       The following duplicate matches were found:
'          TestClass.DisplayValue(String)
'          TestClass.DisplayValue(String, Object[])
'       
'       The following duplicate matches were found:
'          TestClass.Equals(TestClass)
'          TestClass.Equals(Object)
'       
'       TestClass.Equals(TestClass)
'       
'       TestClass.Equals(TestClass, TestClass)

次のいずれかを実行して、特定のメソッドを取得できます。

  • バインド制約を変更します。 前の例では、 型によって宣言され、継承されていないパブリック インスタンス メソッドを取得しようとすると、 が Equals 正常に取得されます Equals(TestClass)

  • メソッドのパラメーターの型を定義するパラメーターを含む メソッドのオーバーロード GetMethod types を呼び出します。

  • メソッドを呼び出して、指定したバインディング属性を持つ型に属するすべてのメソッドを含 GetMethods(BindingFlags) む配列を取得します。 その後、それを反復処理して、 という名前の重複するメソッドを特定できます name 。 この方法は、前の例の例外のハンドラーで示 AmbiguousMatchException されています。

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

現在の がジェネリック型またはジェネリック メソッドの定義の型パラメーターを表している場合、このメソッドはクラス制約のメソッドを検索します。クラス制約がない場合は、 のメソッドを検索します。 Type Object

注意

ジェネリック メソッドの場合は、 に型引数を含め "しない" 必要があります name 。 たとえば、C# コードでは、 型のジェネリック引数を 1 つ持つ という名前のメソッドではなく、"" というテキスト名のメンバー GetMember("MyMethod<int>") MyMethod<int> MyMethod を検索します int

こちらもご覧ください

適用対象

GetMethod(String)

指定した名前のパブリック メソッドを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public System.Reflection.MethodInfo? GetMethod (string name);
public System.Reflection.MethodInfo GetMethod (string name);
member this.GetMethod : string -> System.Reflection.MethodInfo
abstract member GetMethod : string -> System.Reflection.MethodInfo
override this.GetMethod : string -> System.Reflection.MethodInfo
Public Function GetMethod (name As String) As MethodInfo

パラメーター

name
String

取得するパブリック メソッドの名前を格納している文字列。

戻り値

MethodInfo

指定した名前のパブリック メソッドが存在する場合は、そのパブリック メソッドを表すオブジェクト。それ以外の場合は null

実装

例外

指定した名前のメソッドが複数個見つかりました。

namenullです。

次の例では、 という名前のメソッドを取得します MethodA

using namespace System;
using namespace System::Reflection;
public ref class Program
{

    public:

        // Method to get:
        void MethodA() { }

    };

    int main()
    {

        // Get MethodA()
        MethodInfo^ mInfo = Program::typeid->GetMethod("MethodA");
        Console::WriteLine("Found method: {0}", mInfo );

    }

using System;
using System.Reflection;

class Program
{

    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {

        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA");
        Console.WriteLine("Found method: {0}", mInfo);
    }
}

Imports System.Reflection

Class Program

    ' Method to get:
    Public Sub MethodA()
    End Sub


    Public Shared Sub Main(ByVal args() As String)

        ' Get MethodA()
        Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA")
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注釈

の検索では name 、大文字と小文字が区別されます。 検索には、パブリックの静的インスタンス メソッドとパブリック インスタンス メソッドが含まれています。

メソッドがオーバーロードされ、複数のパブリック メソッドがある場合、メソッド GetMethod(String) は例外をスロー AmbiguousMatchException します。 次の例では、 メソッドのパブリック オーバーロードが複数あるため、例外がスロー Int32.ToString されます。 一方、 メソッドは をオーバーライドするため、オーバーロードされないので、 メソッドは オブジェクト Person.ToString Object.ToString GetMethod(String) を取得 MethodInfo できます。

using System;
using System.Reflection;

public class Person
{
   public String FirstName;
   public String LastName;
   
   public override String ToString()
   {
      return (FirstName + " " + LastName).Trim();
   }
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(Person);
      RetrieveMethod(t, "ToString");
      
      t = typeof(Int32);
      RetrieveMethod(t, "ToString");
   }

   private static void RetrieveMethod(Type t, String name)
   {   
      try {
         MethodInfo m = t.GetMethod(name);
         if (m != null) 
            Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
                              m.Name, m.IsStatic ? "Static" : "Instance");    
         else
            Console.WriteLine("{0}.ToString method not found", t.Name);
      }
      catch (AmbiguousMatchException) {
         Console.WriteLine("{0}.{1} has multiple public overloads.", 
                           t.Name, name);
      }
   }
}
// The example displays the following output:
//       Person.ToString: Instance method
//       Int32.ToString has multiple public overloads.
Imports System.Reflection

Public Class Person
   Public FirstName As String
   Public LastName As String
   
   Public Overrides Function ToString() As String
      Return (FirstName + " " + LastName).Trim()
   End Function
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(Person)
      RetrieveMethod(t, "ToString")
      
      t = GetType(Int32)
      RetrieveMethod(t, "ToString")
   End Sub
   
   Private Sub RetrieveMethod(t As Type, name As String)   
      Try
         Dim m As MethodInfo = t.GetMethod(name)
         If m IsNot Nothing Then
            Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
                              m.Name, If(m.IsStatic, "Static", "Instance"))    
         Else
            Console.WriteLine("{0}.ToString method not found", t.Name)
         End If   
      Catch e As AmbiguousMatchException
         Console.WriteLine("{0}.{1} has multiple public overloads.", 
                           t.Name, name)
      End Try
   End Sub
End Module
' The example displays the following output:
'       Person.ToString: Instance method
'       Int32.ToString has multiple public overloads.

次のいずれかを実行して、特定のメソッドを取得できます。

  • メソッドを GetMethod(String, BindingFlags) 呼び出し、メソッド bindingAttr を一意に識別する引数を指定します。 たとえば、型に静的およびインスタンス オーバーロードが含み、例外がスローされた場合は、 の引数を bindingAttr 指定できます BindingFlags.Instance Or BindingFlags.Instance

  • メソッドのパラメーターの型を定義するパラメーターを含む メソッドのオーバーロード GetMethod types を呼び出します。

  • メソッドを GetMethods() 呼び出して、型に属するすべてのパブリック メソッドを含む配列を取得します。 その後、それを反復処理して、 という名前の重複するメソッドを特定できます name

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

現在の がジェネリック型またはジェネリック メソッドの定義の型パラメーターを表している場合、このメソッドはクラス制約のメソッドを検索します。クラス制約がない場合は、 のメソッドを検索します。 Type Object

注意

ジェネリック メソッドの場合は、 に型引数を含め "しない" 必要があります name 。 たとえば、C# コードでは、 型のジェネリック引数を 1 つ持つ という名前のメソッドではなく、"" というテキスト名のメンバー GetMember("MyMethod<int>") MyMethod<int> MyMethod を検索します int

こちらもご覧ください

適用対象

GetMethod(String, Type[], ParameterModifier[])

指定したパブリック メソッドのうち、指定した引数の型および修飾子と一致するパラメーターが設定されているものを検索します。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type(), modifiers As ParameterModifier()) As MethodInfo

パラメーター

name
String

取得するパブリック メソッドの名前を格納している文字列。

types
Type[]

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

または パラメーターをとらないメソッドを取得するための Type オブジェクトの空の配列 (EmptyTypes フィールドで指定)。

modifiers
ParameterModifier[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 COM 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

MethodInfo

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は null

実装

例外

指定された名前および指定されたパラメーターを持つメソッドが複数見つかりました。

namenullです。

または typesnullです。

または types の要素の 1 つが null です。

types が多次元です。

または modifiers が多次元です。

注釈

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

の検索では name 、大文字と小文字が区別されます。 検索には、パブリックの静的インスタンス メソッドとパブリック インスタンス メソッドが含まれています。

注意

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

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

現在の がジェネリック型またはジェネリック メソッドの定義の型パラメーターを表している場合、このメソッドはクラス制約のメソッドを検索します。クラス制約がない場合は、 のメソッドを検索します。 Type Object

注意

ジェネリック メソッドの場合は、 に型引数を含め "しない" 必要があります name 。 たとえば、C# コードでは、 型のジェネリック引数を 1 つ持つ という名前のメソッドではなく、"" というテキスト名のメンバー GetMethod("MyMethod<int>") MyMethod<int> MyMethod を検索します int 。 代わりに、 を GetMethod("MyMethod") 配列の適切なパラメーターと一緒に使用 types します。

こちらもご覧ください

適用対象