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, BindingFlags, Type[])
GetMethod(String, BindingFlags)

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

GetMethod(String, BindingFlags, Type[])

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

GetMethod(String, Int32, Type[])

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

GetMethod(String, Type[])

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

GetMethod(String)

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

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

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

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

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

例外

namenull です。

または

typesnull です。

または

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

genericParameterCount が負の値です。

適用対象

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は 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);
  }
}
open System.Reflection

type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // Get MethodA(int i, int j)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int[] i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int[]> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int* i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakePointerType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(ref int r)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int i, out int o)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {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

注釈

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

次の表は、型に反映するときに、 メソッドによって GetXXX 返される基底クラスのメンバーを示しています。

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

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

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

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

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

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

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

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

検索の動作方法を変更するには、次 BindingFlags の修飾子フラグを使用できます。

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

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

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

注意

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

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

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

注意

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

こちらもご覧ください

適用対象

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

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

例外

namenull です。

または

typesnull です。

または

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

genericParameterCount が負の値です。

適用対象

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は 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);
  }
}
open System
open System.Reflection

type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // Get MethodA(int i, int j)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int[] i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int[]> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int* i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakePointerType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(ref int r)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int i, out int o)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {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

注釈

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

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

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

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

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

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

検索の動作方法を変更するには、次 BindingFlags の修飾子フラグを使用できます。

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

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

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

注意

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

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

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

注意

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

こちらもご覧ください

適用対象

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

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

例外

namenull です。

または

typesnull です。

または

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

genericParameterCount が負の値です。

適用対象

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

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

パラメーター

name
String
genericParameterCount
Int32
bindingAttr
BindingFlags
types
Type[]

戻り値

適用対象

GetMethod(String, BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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

戻り値

指定した要件と一致するメソッドが存在する場合は、そのメソッドを表すオブジェクト。それ以外の場合は 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);
    }
}
open System.Reflection

type Program() =
    // Method to get:
    member _.MethodA() = ()

// Get MethodA()
let mInfo = typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance)
printfn $"Found method: {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.StaticBindingFlags.Instance指定する必要があります。

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

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

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

検索の動作方法を変更するには、次 BindingFlags の修飾子フラグを使用できます。

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

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

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

  • TestClassには、 メソッドの Equals 2 つのパブリック インスタンス オーバーロードがあります。そのうちの 1 つは、 Equals(TestClass)Equals(Object)から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 Example1
{
    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)
open System
open System.Reflection

type TestClass() =
    member _.DisplayValue(s) = 
        printfn $"%s{s}"

    member _.DisplayValue(s: string, [<ParamArray>]values: obj[]) =
        Console.WriteLine(s, values)

    member this.Equals(t: TestClass) =
        Object.ReferenceEquals(this, t)

    static member Equals(t1: TestClass, t2: TestClass) =
        Object.ReferenceEquals(t1, t2)

let retrieveMethod (t: Type) name (flags: BindingFlags) =
    try
        let m = t.GetMethod(name, flags)
        if m <> null then
            printf $"{t.Name}.{m.Name}("
            let parms = m.GetParameters()
            for i = 0 to parms.Length - 1 do
                printf $"{parms[i].ParameterType.Name}"
                if i < parms.Length - 1 then
                    printf ", "
            printfn ")"
        else
            printfn "Method not found"
    with :? AmbiguousMatchException ->
        printfn "The following duplicate matches were found:"
        let methods = t.GetMethods flags
        for method in methods do
            if method.Name = name then
                printf $"   {t.Name}.{method.Name}("
                let parms = method.GetParameters()
                for i = 0 to parms.Length - 1 do
                    printf $"{parms[i].ParameterType.Name}"
                    if i < parms.Length - 1 then
                        printf ", "
                printfn ")"
    printfn ""

let 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)

// 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# コードGetMember("MyMethod<int>")は、 型のジェネリック引数を 1 つ持つ という名前MyMethodのメソッドではなく、"MyMethod<int>" という名前のメンバーを検索しますint

こちらもご覧ください

適用対象

GetMethod(String, BindingFlags, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 提供されます)。

戻り値

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

適用対象

GetMethod(String, Int32, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 フィールドで指定)。

戻り値

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

例外

namenull です。

または

typesnull です。

または

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

genericParameterCount が負の値です。

適用対象

GetMethod(String, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 フィールドで指定)。

戻り値

指定した引数型と一致するパラメーターが設定されたパブリック メソッドが存在する場合は、そのパブリック メソッドを表すオブジェクト。それ以外の場合は 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);
  }
}
type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // member MethodA: i: int * j: int -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int> |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: int[] -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int[]> |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: nativeptr<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakePointerType() |])
    printfn $"Found method: {mInfo}"

    // member MethodA: r: byref<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakeByRefType() |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: int * o: outref<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int>.MakeByRefType() |])
    printfn $"Found method: {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

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

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)
open System
open System.Collections

let getAddMethod (typ: Type) = 
    let method = 
        // Determine if this is a generic type.
        if typ.IsGenericType then
            // Is it an open generic type?
            if typ.ContainsGenericParameters then
                typ.GetMethod("Add", typ.GetGenericArguments())
            // Get closed generic type arguments.
            else
                typ.GetMethod("Add", typ.GenericTypeArguments)
        // This is not a generic type.
        else
            typ.GetMethod("Add", [| typeof<obj> |])

    // Test if an Add method was found.
    if method = null then
        printfn "No Add method found."
    else
        let t = method.ReflectedType
        printf $"{t.Namespace}.{t.Name}.{method.Name}("
        let parms = method.GetParameters()
        for i = 0 to parms.Length - 1 do
            printf $"""{parms[i].ParameterType.Name}{if i < parms.Length - 1 then ", " else ""}"""
        printfn ")"

// Get a Type object that represents a non-generic type.
getAddMethod typeof<ArrayList>

let list = ResizeArray<String>()
// Get a Type object that represents a constructed generic type.
let closed = list.GetType()
getAddMethod closed

// Get a Type object that represents an open generic type.
let opn = typeof<ResizeArray<_>>.GetGenericTypeDefinition()
getAddMethod opn

// 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)

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

注釈

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

注意

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

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

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

注意

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

こちらもご覧ください

適用対象

GetMethod(String)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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

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

戻り値

指定した名前のパブリック メソッドが存在する場合は、そのパブリック メソッドを表すオブジェクト。それ以外の場合は 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);
    }
}
type Program() =
    // Method to get:
    member _.MethodA() = ()

// Get MethodA()
let mInfo = typeof<Program>.GetMethod "MethodA"
printfn $"Found method: {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 Example2
{
    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.
open System
open System.Reflection

type Person() =
    member val FirstName = "" with get, set
    member val LastName = "" with get, set

    override this.ToString() =
        (this.FirstName + " " + this.LastName).Trim()

let retrieveMethod (t: Type) name =
    try
        let m = t.GetMethod name
        if m <> null then
            printfn $"""{m.ReflectedType.Name}.{m.Name}: {if m.IsStatic then "Static" else "Instance"} method"""
        else
            printfn $"{t.Name}.ToString method not found"
    with :? AmbiguousMatchException ->
        printfn $"{t.Name}.{name} has multiple public overloads."

let t = typeof<Person>
retrieveMethod t "ToString"

let t2 = typeof<int>
retrieveMethod t2 "ToString"

// 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 一意に識別する引数を指定します。 たとえば、型に静的オーバーロードとインスタンス オーバーロードがあるために例外がスローされた場合は、 のBindingFlags.InstanceOrBindingFlags.Instance引数をbindingAttr指定できます。

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

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

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

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

注意

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

こちらもご覧ください

適用対象

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

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

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 相互運用機能によって呼び出された場合のみ使用され、参照によって渡されたパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

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

実装

例外

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

namenullです。

または

typesnull です。

または

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

types が多次元です。

または

modifiers が多次元です。

注釈

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

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

注意

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

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

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

注意

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

こちらもご覧ください

適用対象