Type.GetMethod メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の 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[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
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);
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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
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
メソッドのジェネリック型パラメーターの数。
- binder
- Binder
一連のプロパティを定義し、バインディングを有効にするオブジェクト。これには、オーバーロードされたメソッドの選択、引数型の強制型の強制、リフレクションによるメンバーの呼び出しが含まれる場合があります。
-又は-
DefaultBinderを使用するための null 参照 (Visual Basic のNothing)。
- callConvention
- CallingConventions
引数の順序とレイアウト、戻り値の受け渡し方法、引数に使用されるレジスタ、スタックのクリーンアップ方法に関して使用する規則のセットを指定するオブジェクト。
- types
- Type[]
取得するメソッドのパラメーターの数、順序、および型を表す Type オブジェクトの配列。
-又は-
パラメーターを受け取っていないメソッドを取得する Type オブジェクトの空の配列 ( EmptyTypes フィールドによって提供されます)。
- modifiers
- ParameterModifier[]
types配列内の対応する要素に関連付けられた属性を表すParameterModifier オブジェクトの配列。 COM 相互運用機能を介して呼び出すときにのみ使用され、参照によって渡されるパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。
戻り値
指定したジェネリック パラメーター数、引数の型、修飾子、バインド制約、呼び出し規則 (見つかった場合) に一致するメソッドを表すオブジェクト。それ以外の場合は null。
- 属性
例外
genericParameterCount が負の値です。
適用対象
GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
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);
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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
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
取得するメソッドの名前を含む文字列。
- binder
- Binder
一連のプロパティを定義し、バインディングを有効にするオブジェクト。これには、オーバーロードされたメソッドの選択、引数型の強制型の強制、リフレクションによるメンバーの呼び出しが含まれる場合があります。
-又は-
DefaultBinderを使用するための null 参照 (Visual Basic のNothing)。
- callConvention
- CallingConventions
引数の順序とレイアウト、戻り値の受け渡し方法、引数に使用されるレジスタ、スタックのクリーンアップ方法に関して使用する規則のセットを指定するオブジェクト。
- types
- Type[]
取得するメソッドのパラメーターの数、順序、および型を表す Type オブジェクトの配列。
-又は-
パラメーターを受け取っていないメソッドを取得する Type オブジェクトの空の配列 ( EmptyTypes フィールドによって提供されます)。
- modifiers
- ParameterModifier[]
types配列内の対応する要素に関連付けられた属性を表すParameterModifier オブジェクトの配列。 COM 相互運用機能を介して呼び出すときにのみ使用され、参照によって渡されるパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。
戻り値
指定した要件に一致するメソッドを表すオブジェクト (見つかった場合)。それ以外の場合は null。
実装
- 属性
例外
指定した名前を持ち、指定したバインディング制約に一致する複数のメソッドが見つかりました。
例
次の例では、バインド制約、呼び出し規則、およびさまざまな引数型を指定して、 MethodAの特定のオーバーロードを検索します。
注
Visual C# 2005 の例では、 /unsafe コンパイラ オプションが必要です。
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
注釈
既定のバインダーは ParameterModifier ( modifiers パラメーター) を処理しませんが、抽象 System.Reflection.Binder クラスを使用して、 modifiersを処理するカスタム バインダーを記述できます。
ParameterModifier は COM 相互運用機能を介して呼び出すときにのみ使用され、参照渡しのパラメーターのみが処理されます。
次の表は、型に反映するときに、 GetXXX メソッドによって返される基底クラスのメンバーを示しています。
| メンバーの種類 | スタティック | 非静的 |
|---|---|---|
| コンストラクター | いいえ | いいえ |
| フィールド | いいえ | Yes. フィールドは常に名前と署名によって非表示になります。 |
| イベント | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
| メソッド | いいえ | Yes. メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。 |
| 入れ子にされた型 | いいえ | いいえ |
| 財産 | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
名前と署名による隠蔽は、カスタム修飾子、戻り値の型、パラメーター型、センチネル、そしてアンマネージド呼び出し規則を含む、署名のすべての部分を考慮します。 これはバイナリ比較です。
リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。
カスタム属性は、共通型システムの一部ではありません。
次の 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>")は、int型のジェネリック引数を 1 つ持つ MyMethod という名前のメソッドではなく、"MyMethod<int>" という名前のメンバーを検索します。
こちらもご覧ください
- MethodInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
適用対象
GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
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);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
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
メソッドのジェネリック型パラメーターの数。
- binder
- Binder
一連のプロパティを定義し、バインディングを有効にするオブジェクト。これには、オーバーロードされたメソッドの選択、引数型の強制型の強制、リフレクションによるメンバーの呼び出しが含まれる場合があります。
-又は-
DefaultBinderを使用するための null 参照 (Visual Basic のNothing)。
- types
- Type[]
取得するメソッドのパラメーターの数、順序、および型を表す Type オブジェクトの配列。
-又は-
パラメーターを受け取っていないメソッドを取得する Type オブジェクトの空の配列 ( EmptyTypes フィールドによって提供されます)。
- modifiers
- ParameterModifier[]
types配列内の対応する要素に関連付けられた属性を表すParameterModifier オブジェクトの配列。 COM 相互運用機能を介して呼び出すときにのみ使用され、参照によって渡されるパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。
戻り値
指定したジェネリック パラメーター数、引数の型、修飾子、バインド制約 (見つかった場合) に一致するメソッドを表すオブジェクト。それ以外の場合は null。
- 属性
例外
genericParameterCount が負の値です。
適用対象
GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
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);
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
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
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
取得するメソッドの名前を含む文字列。
- binder
- Binder
一連のプロパティを定義し、バインディングを有効にするオブジェクト。これには、オーバーロードされたメソッドの選択、引数型の強制型の強制、リフレクションによるメンバーの呼び出しが含まれる場合があります。
-又は-
DefaultBinderを使用するための null 参照 (Visual Basic のNothing)。
- types
- Type[]
取得するメソッドのパラメーターの数、順序、および型を表す Type オブジェクトの配列。
-又は-
パラメーターを受け取っていないメソッドを取得する Type オブジェクトの空の配列 ( EmptyTypes フィールドによって提供されます)。
- modifiers
- ParameterModifier[]
types配列内の対応する要素に関連付けられた属性を表すParameterModifier オブジェクトの配列。 COM 相互運用機能を介して呼び出すときにのみ使用され、参照によって渡されるパラメーターのみが処理されます。 既定のバインダーでは、このパラメーターは処理されません。
戻り値
指定した要件に一致するメソッドを表すオブジェクト (見つかった場合)。それ以外の場合は null。
実装
- 属性
例外
指定した名前を持ち、指定したバインディング制約に一致する複数のメソッドが見つかりました。
例
次の例では、バインド制約とさまざまな引数型を指定して、 MethodAの特定のオーバーロードを検索します。
注
Visual C# 2005 の例では、 /unsafe コンパイラ オプションが必要です。
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
注釈
既定のバインダーは ParameterModifier ( modifiers パラメーター) を処理しませんが、抽象 System.Reflection.Binder クラスを使用して、 modifiersを処理するカスタム バインダーを記述できます。
ParameterModifier は 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# コードGetMember("MyMethod<int>")は、int型のジェネリック引数を 1 つ持つ MyMethod という名前のメソッドではなく、"MyMethod<int>" という名前のメンバーを検索します。
こちらもご覧ください
- MethodInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
適用対象
GetMethod(String, Int32, Type[], ParameterModifier[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
指定したジェネリック パラメーター数、引数の型、および修飾子と一致するパラメーターを持つ、指定したパブリック メソッドを検索します。
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
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);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
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。
- 属性
例外
genericParameterCount が負の値です。
適用対象
GetMethod(String, Int32, BindingFlags, Type[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
指定したバインディング制約を使用して、指定したジェネリック パラメーターの数と引数の型と一致するパラメーターを持つ、指定したメソッドを検索します。
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
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
メソッドのジェネリック型パラメーターの数。
- types
- Type[]
取得するメソッドのパラメーターの数、順序、および型を表す Type オブジェクトの配列。
-又は-
パラメーターを受け取っていないメソッドを取得する Type オブジェクトの空の配列 ( EmptyTypes フィールドによって提供されます)。
戻り値
指定したジェネリック パラメーター数、引数の型、バインド制約 (見つかった場合) と一致するメソッドを表すオブジェクト。それ以外の場合は null。
- 属性
適用対象
GetMethod(String, BindingFlags)
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
指定したバインディング制約を使用して、指定したメソッドを検索します。
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
abstract member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
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
取得するメソッドの名前を含む文字列。
戻り値
指定した要件に一致するメソッドを表すオブジェクト (見つかった場合)。それ以外の場合は null。
実装
- 属性
例外
指定した名前を持ち、指定したバインディング制約に一致する複数のメソッドが見つかりました。
name は nullです。
例
次の例では、指定したバインド フラグに一致するメソッドを取得します。
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.Instance または BindingFlags.Static を指定する必要があります。
検索にパブリック メソッドを含める BindingFlags.Public を指定します。
BindingFlags.NonPublicを指定して、非パブリック メソッド (プライベート、内部、および保護されたメソッド) を検索に含めます。
BindingFlags.FlattenHierarchyおよび
public静的メンバーを階層に含めるprotectedを指定します。継承されたクラスprivate静的メンバーは含まれません。
次の BindingFlags 修飾子フラグを使用して、検索の動作を変更できます。
BindingFlags.IgnoreCase の大文字と小文字を無視するには
name。BindingFlags.DeclaredOnly 単に継承されたメソッドではなく、 Typeで宣言されたメソッドのみを検索する場合。
詳細については、System.Reflection.BindingFlags を参照してください。
メソッドがオーバーロードされ、複数のオーバーロードが bindingAttr 引数で指定された制約を満たしている場合、メソッドは AmbiguousMatchException 例外をスローします。 次の例では、次の理由から例外がスローされます。
TestClass型には、DisplayValueメソッドの 2 つのパブリック インスタンス オーバーロード (DisplayValue(String)とDisplayValue(String, Object[])) があります。TestClass型には、Equalsメソッドの 2 つのパブリック インスタンス オーバーロードがあり、そのうちの 1 つは 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 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)が正常に取得されます。メソッドのパラメーターの型を定義する
typesパラメーターを含むGetMethod メソッドのオーバーロードを呼び出します。GetMethods(BindingFlags) メソッドを呼び出して、指定したバインディング属性を持つ型に属するすべてのメソッドを含む配列を取得します。 その後、それを反復処理して、
nameという名前の重複するメソッドを識別できます。 この方法は、 AmbiguousMatchException 例外に対する前の例のハンドラーで示されています。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで MethodInfo を返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のメソッド、またはクラス制約がない場合は Object のメソッドを検索します。
注
ジェネリック メソッドの場合は、 nameに型引数を含めないでください。 たとえば、C# コードGetMember("MyMethod<int>")は、int型のジェネリック引数を 1 つ持つ MyMethod という名前のメソッドではなく、"MyMethod<int>" という名前のメンバーを検索します。
こちらもご覧ください
- MethodInfo
- String
- BindingFlags
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
適用対象
GetMethod(String, BindingFlags, Type[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
指定したバインディング制約を使用して、指定した引数の型と一致するパラメーターを持つ指定したメソッドを検索します。
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
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[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
指定したジェネリック パラメーターの数と引数の型と一致するパラメーターを持つ、指定したパブリック メソッドを検索します。
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * Type[] -> System.Reflection.MethodInfo
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。
- 属性
例外
genericParameterCount が負の値です。
適用対象
GetMethod(String, Type[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types);
public System.Reflection.MethodInfo GetMethod(string name, Type[] types);
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
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。
実装
- 属性
例外
指定した名前と指定されたパラメーターを持つ複数のメソッドが見つかりました。
types は多次元です。
例
次の例では、さまざまな引数型を指定して、 MethodAの特定のオーバーロードを検索します。
注
Visual C# 2005 の例では、 /unsafe コンパイラ オプションが必要です。
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> クラス)、および閉じたジェネリック型 (List(Of String)型) のAdd メソッドを表す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>")は、int型のジェネリック引数が 1 つ含まれる MyGenericMethod という名前のメソッドではなく、"MyGenericMethod<int>" という名前のメソッドを検索します。 代わりに、types配列内の適切なパラメーターでGetMethod("MyGenericMethod")を使用します。
こちらもご覧ください
- MethodInfo
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
適用対象
GetMethod(String)
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
指定した名前のパブリック メソッドを検索します。
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name);
public System.Reflection.MethodInfo GetMethod(string name);
public System.Reflection.MethodInfo? GetMethod(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string -> System.Reflection.MethodInfo
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。
実装
- 属性
例外
指定した名前の複数のメソッドが見つかりました。
name は nullです。
例
次の例では、 MethodAという名前のメソッドを取得します。
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.StaticまたはBindingFlags.InstanceのbindingAttr引数を指定できます。メソッドのパラメーターの型を定義する
typesパラメーターを含むGetMethod メソッドのオーバーロードを呼び出します。GetMethods() メソッドを呼び出して、型に属するすべてのパブリック メソッドを含む配列を取得します。 その後、それを反復処理して、
nameという名前の重複するメソッドを識別できます。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで MethodInfo を返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のメソッド、またはクラス制約がない場合は Object のメソッドを検索します。
注
ジェネリック メソッドの場合は、 nameに型引数を含めないでください。 たとえば、C# コードGetMember("MyMethod<int>")は、int型のジェネリック引数を 1 つ持つ MyMethod という名前のメソッドではなく、"MyMethod<int>" という名前のメンバーを検索します。
こちらもご覧ください
- MethodInfo
- String
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
適用対象
GetMethod(String, Type[], ParameterModifier[])
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- 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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
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);
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
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。
実装
- 属性
例外
指定した名前と指定されたパラメーターを持つ複数のメソッドが見つかりました。
注釈
既定のバインダーは ParameterModifier ( modifiers パラメーター) を処理しませんが、抽象 System.Reflection.Binder クラスを使用して、 modifiersを処理するカスタム バインダーを記述できます。
ParameterModifier は COM 相互運用機能を介して呼び出すときにのみ使用され、参照渡しのパラメーターのみが処理されます。
nameの検索では、大文字と小文字が区別されます。 検索には、パブリック静的メソッドとパブリック インスタンス メソッドが含まれます。
注
コンストラクターとメソッドを参照するときにパラメーターを省略することはできません。 パラメーターは、呼び出し時にのみ省略できます。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで MethodInfo を返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のメソッド、またはクラス制約がない場合は Object のメソッドを検索します。
注
ジェネリック メソッドの場合は、 nameに型引数を含めないでください。 たとえば、C# コードGetMethod("MyMethod<int>")は、int型のジェネリック引数を 1 つ持つ MyMethod という名前のメソッドではなく、"MyMethod<int>" という名前のメンバーを検索します。 代わりに、types配列内の適切なパラメーターでGetMethod("MyMethod")を使用します。
こちらもご覧ください
- MethodInfo
- String
- DefaultBinder
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()