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

Type.GetMethod メソッド (String, BindingFlags)

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

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

Public Function GetMethod ( _
    name As String, _
    bindingAttr As BindingFlags _
) As MethodInfo
Dim instance As Type
Dim name As String
Dim bindingAttr As BindingFlags
Dim returnValue As MethodInfo

returnValue = instance.GetMethod(name, bindingAttr)
public MethodInfo GetMethod (
    string name,
    BindingFlags bindingAttr
)
public:
virtual MethodInfo^ GetMethod (
    String^ name, 
    BindingFlags bindingAttr
) sealed
public final MethodInfo GetMethod (
    String name, 
    BindingFlags bindingAttr
)
public final function GetMethod (
    name : String, 
    bindingAttr : BindingFlags
) : MethodInfo

パラメータ

name

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

bindingAttr

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

または

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

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

例外例外
例外の種類条件

AmbiguousMatchException

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

ArgumentNullException

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

解説解説

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

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

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

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

メモメモ

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

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

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

メモメモ

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

使用例使用例

指定したバインディング フラグに一致するメソッドを取得し、そのシグネチャを表示する例を次に示します。

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class [MyClass]

    Public Sub MyMethod()
    End Sub 'MyMethod
End Class '[MyClass]
Public Class Type_GetMethod
    Public Shared Sub Main()
        Try
            ' Get the type of MyClass.
            Dim myType As Type = GetType([MyClass])

            ' Get the method information for MyClass.
            Dim myMethodInfo As MethodInfo
 = myType.GetMethod("MyMethod", BindingFlags.Public
 Or BindingFlags.Instance)

            ' Display the method information.
            Console.WriteLine(ControlChars.Cr + "The signature
 of MyMethod is: {0}", myMethodInfo.ToString())
        Catch e As Exception
            Console.WriteLine("Exception: {0}", e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'Type_GetMethod
using System;
using System.Reflection;

class MyClass
{
    public void MyMethod()
    {    
    }
}

public class Type_GetMethod
{
    public static void Main()
    {
        try
        {
            // Get the type of MyClass.
            Type myType = typeof(MyClass);
         
            // Get the method information for MyClass.
            MethodInfo myMethodInfo = myType.GetMethod("MyMethod",
                BindingFlags.Public|BindingFlags.Instance );
         
            // Display the method information.
            Console.WriteLine("\nThe signature of MyMethod is: {0}", myMethodInfo.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: {0}", e.Message);
        }
    }
}
using namespace System;
using namespace System::Reflection;
ref class MyClass
{
public:
   void MyMethod(){}
};

int main()
{
   try
   {
      // Get the type of MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get the method information for MyClass.
      MethodInfo^ myMethodInfo = myType->GetMethod( "MyMethod", static_cast<BindingFlags>(BindingFlags::Public
 | BindingFlags::Instance) );
      
      // Display the method information.
      Console::WriteLine( "\nThe signature of MyMethod is: {0}", myMethodInfo
 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
import System.*;
import System.Reflection.*;

class MyClass
{
    public void MyMethod()
    {
    } //MyMethod
} //MyClass

public class Type_GetMethod
{
    public static void main(String[]
 args)
    {
        try {
            // Get the type of MyClass.
            Type myType = MyClass.class.ToType();
            // Get the method information for MyClass.
            MethodInfo myMethodInfo = myType.GetMethod("MyMethod",
                BindingFlags.Public | BindingFlags.Instance);
            // Display the method information.
            Console.WriteLine("\nThe signature of MyMethod is: {0}",
                myMethodInfo.ToString());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: {0}", e.get_Message());
        }
    } //main
} //Type_GetMethod
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
MethodInfo
String クラス
BindingFlags
DefaultBinder
ReflectionPermission
GetMethodImpl
GetMethods

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

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

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

Public Function GetMethod ( _
    name As String, _
    bindingAttr As BindingFlags, _
    binder As Binder, _
    callConvention As CallingConventions, _
    types As Type(), _
    modifiers As ParameterModifier() _
) As MethodInfo
Dim instance As Type
Dim name As String
Dim bindingAttr As BindingFlags
Dim binder As Binder
Dim callConvention As CallingConventions
Dim types As Type()
Dim modifiers As ParameterModifier()
Dim returnValue As MethodInfo

returnValue = instance.GetMethod(name, bindingAttr, binder, callConvention, types,
 modifiers)
public MethodInfo GetMethod (
    string name,
    BindingFlags bindingAttr,
    Binder binder,
    CallingConventions callConvention,
    Type[] types,
    ParameterModifier[] modifiers
)
public:
virtual MethodInfo^ GetMethod (
    String^ name, 
    BindingFlags bindingAttr, 
    Binder^ binder, 
    CallingConventions callConvention, 
    array<Type^>^ types, 
    array<ParameterModifier>^ modifiers
) sealed
public final MethodInfo GetMethod (
    String name, 
    BindingFlags bindingAttr, 
    Binder binder, 
    CallingConventions callConvention, 
    Type[] types, 
    ParameterModifier[] modifiers
)
public final function GetMethod (
    name : String, 
    bindingAttr : BindingFlags, 
    binder : Binder, 
    callConvention : CallingConventions, 
    types : Type[], 
    modifiers : ParameterModifier[]
) : MethodInfo

パラメータ

name

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

bindingAttr

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

または

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

binder

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

または

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

callConvention

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

types

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

または

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

modifiers

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

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

例外例外
例外の種類条件

AmbiguousMatchException

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

ArgumentNullException

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

または

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

または

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

ArgumentException

types が多次元です。

または

modifiers が多次元です。

または

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

解説解説

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

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

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

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

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

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

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

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

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

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

メモメモ

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

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

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

メモメモ

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

使用例使用例

クラス MyClass の MyFunc という名前のメソッドに関する情報を表示する例を次に示します。

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class [MyClass]
    Public i As Integer
 = 10
    Public j As Integer
 = 2
    Public Function MyFunc(ByVal
 i As Integer, ByVal j
 As Integer) As Integer
        Dim k As Integer
        k = i * 10 - j
        Return k
    End Function 'MyFunc
End Class '[MyClass]

Public Class Type_GetMethod
    Public Shared Sub Main()
        Try
            ' Get the type of MyClass.
            Dim myType As Type = GetType([MyClass])
            ' Get information for MyFunc(int, int).
            Dim myMemberInfo As MemberInfo
 = myType.GetMethod("MyFunc", BindingFlags.Public
 Or BindingFlags.Instance, Nothing, CallingConventions.Any, New Type() {GetType(Integer),
 GetType(Integer)}, Nothing)
            Console.WriteLine(ControlChars.NewLine + "Displaying
 method MyFunc: " + ControlChars.NewLine)
            ' Display the method information.
            Console.WriteLine("{0}", myMemberInfo)
        Catch e As Exception
            Console.WriteLine("Exception : {0}", e.Message.ToString())
        End Try
    End Sub 'Main '
End Class 'Type_GetMethod
using System;
using System.Reflection;

public class MyClass
{
    public int i = 10;
    public int j = 2;
    public int MyFunc(int
 i , int j)
    {
        int k;
        k = i * 10 - j;
        return k;
    }
}
public class Type_GetMethod
{
    public static void Main()
    {
        try
        {
            // Get the type of MyClass.
            Type myType = typeof(MyClass);
            // Get the method information for MyFunc(int, int).
            MemberInfo myMemberInfo = myType.GetMethod("MyFunc", BindingFlags.Public
 |
                BindingFlags.Instance, 
                null, 
                CallingConventions.Any,  
                new Type[] {typeof(int),typeof(int)},
 
                null);
            Console.WriteLine("\nDisplaying information for
 MyFunc: \n");
            // Display the method information.
            Console.WriteLine("{0}", myMemberInfo);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception : {0}", e.Message);
        }
    }
}
using namespace System;
using namespace System::Reflection;
public ref class MyClass
{
public:
   int i;
   int j;
   int MyFunc( int i, int
 j )
   {
      int k;
      k = i * 10 - j;
      return k;
   }
};

int main()
{
   try
   {
      // Get the type of MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get the method information for MyFunc(int, int).
      array<Type^>^temp0 = {int::typeid,int::typeid};
      MemberInfo^ myMemberInfo = myType->GetMethod( "MyFunc", static_cast<BindingFlags>(BindingFlags::Public
 | BindingFlags::Instance), nullptr, CallingConventions::Any, temp0, nullptr );
      Console::WriteLine( "\nDisplaying information for MyFunc:
 \n" );
      
      // Display the method information.
      Console::WriteLine( "{0}", myMemberInfo );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e->Message );
   }
}
import System.*;
import System.Reflection.*;

public class MyClass
{
    public int i = 10;
    public int j = 2;

    public int MyFunc(int
 i, int j)
    {
        int k;
        k = i * 10 - j;
        return k;
    } //MyFunc
} //MyClass

public class Type_GetMethod
{
    public static void main(String[]
 args)
    {
        try {
            // Get the type of MyClass.
            Type myType = MyClass.class.ToType();
            // Get the method information for MyFunc(int, int).
            MemberInfo myMemberInfo = myType.GetMethod("MyFunc",
                BindingFlags.Public | BindingFlags.Instance, null
,
                CallingConventions.Any,
                new Type[] { int.class.ToType(),
 int.class.ToType() }, null);
            Console.WriteLine("\nDisplaying information for
 MyFunc: \n");
            // Display the method information.
            Console.WriteLine("{0}", myMemberInfo);
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : {0}", e.get_Message());
        }
    } //main
} //Type_GetMethod
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
MethodInfo
String クラス
BindingFlags
Binder
DefaultBinder
CallingConventions
ParameterModifier
ReflectionPermission
GetMethodImpl
GetMethods

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

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

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

Public Function GetMethod ( _
    name As String, _
    bindingAttr As BindingFlags, _
    binder As Binder, _
    types As Type(), _
    modifiers As ParameterModifier() _
) As MethodInfo
Dim instance As Type
Dim name As String
Dim bindingAttr As BindingFlags
Dim binder As Binder
Dim types As Type()
Dim modifiers As ParameterModifier()
Dim returnValue As MethodInfo

returnValue = instance.GetMethod(name, bindingAttr, binder, types, modifiers)
public MethodInfo GetMethod (
    string name,
    BindingFlags bindingAttr,
    Binder binder,
    Type[] types,
    ParameterModifier[] modifiers
)
public:
virtual MethodInfo^ GetMethod (
    String^ name, 
    BindingFlags bindingAttr, 
    Binder^ binder, 
    array<Type^>^ types, 
    array<ParameterModifier>^ modifiers
) sealed
public final MethodInfo GetMethod (
    String name, 
    BindingFlags bindingAttr, 
    Binder binder, 
    Type[] types, 
    ParameterModifier[] modifiers
)
public final function GetMethod (
    name : String, 
    bindingAttr : BindingFlags, 
    binder : Binder, 
    types : Type[], 
    modifiers : ParameterModifier[]
) : MethodInfo

パラメータ

name

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

bindingAttr

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

または

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

binder

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

または

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

types

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

または

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

modifiers

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

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

例外例外
例外の種類条件

AmbiguousMatchException

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

ArgumentNullException

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

または

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

または

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

ArgumentException

types が多次元です。

または

modifiers が多次元です。

または

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

解説解説

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

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

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

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

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

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

メモメモ

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

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

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

メモメモ

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

使用例使用例

メソッド情報を取得し、シグネチャと宣言型を表示する例を次に示します。

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class [MyClass]
    Public i As Integer
 = 10
    Public k As String =
 "My Field"
    Public Overloads Function
 MyMethod(ByVal i As Integer)
 As Integer
        Me.i = i
        Return i
    End Function 'MyMethod
    Public Overloads Function
 MyMethod(ByVal k As String)
 As String
        Me.k = k
        Return k
    End Function 'MyMethod
End Class '[MyClass]
Public Class Type_GetMethod
    Public Shared Sub Main()
        Try
            ' Get the type of MyClass.
            Dim myType As Type = GetType([MyClass])
            ' Get the attributes and metadata of MyMethod.
            Dim myMethodInfo As MethodInfo
 = myType.GetMethod("MyMethod", BindingFlags.Public
 Or BindingFlags.Instance, Nothing, New Type() {GetType(Integer)}, Nothing)
            Console.WriteLine(ControlChars.Cr + "Declaring type
 of the method {0} is : " + " {1}", myMethodInfo,
 myMethodInfo.DeclaringType.ToString())
            ' Get the attributes and metadata of MyMethod.
            Dim myMethodInfo1 As MethodInfo
 = myType.GetMethod("MyMethod", BindingFlags.Public
 Or BindingFlags.Instance, Nothing, New Type() {GetType(String)}, Nothing)
            Console.WriteLine(ControlChars.Cr + "Declaring type
 of the method {0} is : " + " {1}", myMethodInfo1,
 myMethodInfo1.DeclaringType.ToString())
        Catch e As Exception
            Console.WriteLine("Exception: {0}", e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'Type_GetMethod
using System;
using System.Reflection;

public class MyClass
{
    public int i = 10;
    public string k = "My Field";
    public int MyMethod(int
 i)
    {
        this.i = i;
        return i;
    }
    public string MyMethod(string
 k)
    {
        this.k = k;
        return k;
    }
}
public class Type_GetMethod
{
    public static void Main()
    {
        try
        {
            // Get the type of MyClass.
            Type myType = typeof(MyClass);
            // Get the attributes and metadata of MyMethod.
            MethodInfo myMethodInfo = myType.GetMethod("MyMethod", 
                BindingFlags.Public | BindingFlags.Instance, null
,
 
                new Type[] {typeof(int)}, null);
            Console.WriteLine("\n Declaring type of the method {0} is: \n {1}"
,
 myMethodInfo,
                myMethodInfo.DeclaringType);
            // Get the attributes and metadata of MyMethod.
            MethodInfo myMethodInfo1 = myType.GetMethod("MyMethod", 
                BindingFlags.Public | BindingFlags.Instance, null
,
 
                new Type[] {typeof(string)},
 null);
            Console.WriteLine("\n Declaring type of the method {0} is: \n {1}"
,
 myMethodInfo1,
                myMethodInfo1.DeclaringType);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: {0}", e.Message);
        }
    }
}
using namespace System;
using namespace System::Reflection;
public ref class MyClass
{
public:
   int i;
   String^ k;
   int MyMethod( int i )
   {
      this->i = i;
      return i;
   }

   String^ MyMethod( String^ k )
   {
      this->k = k;
      return k;
   }

};

int main()
{
   try
   {
      // Get the type of MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get the attributes and metadata of MyMethod.
      array<Type^>^temp0 = {int::typeid};
      MethodInfo^ myMethodInfo = myType->GetMethod( "MyMethod", static_cast<BindingFlags>(BindingFlags::Public
 | BindingFlags::Instance), nullptr, temp0, nullptr );
      Console::WriteLine( "\n Declaring type of the method {0} is: \n {1}",
 myMethodInfo, myMethodInfo->DeclaringType );
      
      // Get the attributes and metadata of MyMethod.
      array<Type^>^temp1 = {String::typeid};
      MethodInfo^ myMethodInfo1 = myType->GetMethod( "MyMethod", static_cast<BindingFlags>(BindingFlags::Public
 | BindingFlags::Instance), nullptr, temp1, nullptr );
      Console::WriteLine( "\n Declaring type of the method {0} is: \n {1}",
 myMethodInfo1, myMethodInfo1->DeclaringType );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
import System.*;
import System.Reflection.*;

public class MyClass
{
    public int i = 10;
    public String k = "My Field";

    public int MyMethod(int
 i)
    {
        this.i = i;
        return i;
    } //MyMethod

    public String MyMethod(String k)
    {
        this.k = k;
        return k;
    } //MyMethod
} //MyClass

public class Type_GetMethod
{
    public static void main(String[]
 args)
    {
        try {
            // Get the type of MyClass.
            Type myType = MyClass.class.ToType();
            // Get the attributes and metadata of MyMethod.
            MethodInfo myMethodInfo = myType.GetMethod("MyMethod",
                BindingFlags.Public | BindingFlags.Instance, null
,
                new Type[] { int.class.ToType()
 }, null);
            Console.WriteLine("\n Declaring type of the method {0} is: \n {1}"
,
                myMethodInfo, myMethodInfo.get_DeclaringType());
            // Get the attributes and metadata of MyMethod.
            MethodInfo myMethodInfo1 = myType.GetMethod("MyMethod",
                BindingFlags.Public | BindingFlags.Instance, null
,
                new Type[] { String.class.ToType()
 }, null);
            Console.WriteLine("\n Declaring type of the method {0} is: \n {1}"
,
                myMethodInfo1, myMethodInfo1.get_DeclaringType());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: {0}", e.get_Message());
        }
    } //main
} //Type_GetMethod
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
MethodInfo
String クラス
BindingFlags
Binder
DefaultBinder
ParameterModifier
ReflectionPermission
GetMethodImpl
GetMethods

Type.GetMethod メソッド (String, Type[])

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

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

例外例外
例外の種類条件

AmbiguousMatchException

指定した名前を持ち、指定したパラメータが設定されているメソッドが 2 つ以上存在します。

ArgumentNullException

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

または

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

または

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

ArgumentException

types が多次元です。

解説解説
使用例使用例

オーバーロードされたメソッドを持つクラスの例を次に示します。MyClass1 に対応する Type オブジェクトを取得し、GetMethod を呼び出します。このメソッドの呼び出しにより、パラメータが型の配列で指定した引数と一致するメソッドが返されます。

Imports System
Imports System.Reflection

Public Class MyClass1
    Public Overloads Sub
 MyMethod(ByVal i As Integer)
    End Sub
    Public Overloads Sub
 MyMethod(ByVal i As Integer,
 ByVal j As Integer)
    End Sub
End Class
Public Class TestMyClass
    Public Shared Sub Main()
        Try
            ' Get a Type object corresponding to MyClass1.
            Dim myClassType As Type = GetType(MyClass1)
            ' Create a Type array.
            Dim typeArray(0) As Type
            typeArray.SetValue(GetType(Integer),
 0)
            ' Find the method matching the specified argument types.
            Dim myMethodInfo As MethodInfo
 = myClassType.GetMethod("MyMethod", typeArray)
            Console.WriteLine("The name of the Method :"
 & myMethodInfo.ToString())
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class
using System;
using System.Reflection;

public class MyClass1
{
    public void MyMethod(int
 i)
    {
    }
    public void MyMethod(int
 i,int j)
    {
    }
}
public class TestMyClass
{
    public static void Main()
    {
        try
        {
            // Get a Type object corresponding to MyClass1.
            Type myClassType = typeof(MyClass1);
            // Create a Type array.
            Type[] typeArray =new Type[1];
            typeArray.SetValue(typeof(int),0);
            // Find the method matching the specified argument types.
            MethodInfo myMethodInfo = 
                myClassType.GetMethod("MyMethod", typeArray);
            Console.WriteLine("The name of the method :" + myMethodInfo);
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
using namespace System;
using namespace System::Reflection;
public ref class MyClass1
{
public:
   void MyMethod( int ){}

   void MyMethod( int, int
 ){}
};

int main()
{
   try
   {
      // Get a Type object corresponding to MyClass1.
      Type^ myClassType = MyClass1::typeid;
      
      // Create a Type array.
      array<Type^>^typeArray = gcnew array<Type^>(1);
      typeArray->SetValue( int::typeid, 0 );
      
      // Find the method matching the specified argument types.
      MethodInfo^ myMethodInfo = myClassType->GetMethod( "MyMethod",
 typeArray );
      Console::WriteLine( "The name of the method : {0}", myMethodInfo
 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }
}
import System.*;
import System.Reflection.*;

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

    public void MyMethod(int
 i, int j)
    {
    } //MyMethod
} //MyClass1

public class TestMyClass
{
    public static void main(String[]
 args)
    {
        try {
            // Get a Type object corresponding to MyClass1.
            Type myClassType = MyClass1.class.ToType();
            // Create a Type array.
            Type typeArray[] = new Type[1];
            typeArray.SetValue(int.class.ToType(),
 0);
            // Find the method matching the specified argument types.
            MethodInfo myMethodInfo =
                myClassType.GetMethod("MyMethod", typeArray);
            Console.WriteLine("The name of the method :" + myMethodInfo);
        }
        catch (System.Exception e) {
            Console.WriteLine(e.get_Message());
        }
    } //main
} //TestMyClass
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
MethodInfo
String クラス
DefaultBinder
ReflectionPermission
GetMethodImpl
GetMethods

Type.GetMethod メソッド (String)

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

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

Public Function GetMethod ( _
    name As String _
) As MethodInfo
Dim instance As Type
Dim name As String
Dim returnValue As MethodInfo

returnValue = instance.GetMethod(name)
public MethodInfo GetMethod (
    string name
)
public:
virtual MethodInfo^ GetMethod (
    String^ name
) sealed
public final MethodInfo GetMethod (
    String name
)
public final function GetMethod (
    name : String
) : MethodInfo

パラメータ

name

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

戻り値
指定した名前のパブリック メソッドが存在する場合は、そのパブリック メソッドを表す MethodInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

AmbiguousMatchException

指定した名前のメソッドが 2 つ以上存在します。

ArgumentNullException

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

解説解説
使用例使用例

MyClass で MyFunction のパラメータを取得し、それらの位置とデータ型を表示する例を次に示します。

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

Class [MyClass]
    Public myInt As Integer
 = 0
    Public myString As String
 = Nothing
    Public Sub New()
    End Sub 'New
    Public Sub Myfunction(ByVal
 i As Integer)
    End Sub 'Myfunction
End Class '[MyClass]

Class Type_GetMethod
    Public Shared Sub Main()
        Try
            Dim MyObject As New
 [MyClass]()
            Dim myMethodInfo As MethodInfo

            ' Get the type of MyClass.
            Dim myType As Type = MyObject.GetType()

            ' Get the method information for MyFunction. 
            myMethodInfo = myType.GetMethod("Myfunction")

            ' Get the parameters for Myfunction.
            Dim myParameters As ParameterInfo()
 = myMethodInfo.GetParameters()

            Console.WriteLine(ControlChars.Cr + "The parameters
 of the method Myfunction of class MyClass are:" + ControlChars.Cr)

            ' Display the position and type of the parameters.
            Dim i As Integer
            For i = 0 To myParameters.Length
 - 1
                Console.WriteLine("The data type of parameter
 {0} is {1}.", myParameters(i).Position + 1, myParameters(i).ParameterType)
            Next i
        Catch e As SecurityException
            Console.WriteLine(("SecurityException: "
 + e.Message.ToString()))
        Catch e As Exception
            Console.WriteLine(("Exception: " + e.Message.ToString()))
        End Try
    End Sub 'Main 
End Class 'Type_GetMethod
using System;
using System.Reflection;
using System.Security; 

class MyClass
{
    public int myInt = 0;
    public string myString = null;
    public MyClass()
    {
    }
    public void Myfunction(int
 i)
    {
    } 
}
class Type_GetMethod
{
    public static void Main()
    {
        try
        {         
            MyClass MyObject = new MyClass();
            MethodInfo myMethodInfo; 

            // Get the type of MyClass.
            Type myType = MyObject.GetType(); 
        
            // Get the method information for MyFunction. 
            myMethodInfo = myType.GetMethod("Myfunction");
         
            // Get the parameters for Myfunction.
            ParameterInfo[] myParameters = myMethodInfo.GetParameters();
    
            Console.WriteLine( "\nThe parameters of the method Myfunction of
 class MyClass are :\n"); 
         
            // Display the position and type of the parameters.
            for(int i = 0; i < myParameters.Length;
 i++)
                Console.WriteLine("The data type of parameter {0} is {1}."
,
 
                    myParameters[i].Position + 1, myParameters[i].ParameterType);
        }
        catch (SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message ); 
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message ); 
        }      
    }
}
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
ref class MyClass
{
public:
   int * myInt;
   String^ myString;
   MyClass(){}

   void Myfunction( int i )
   {
      myString = i.ToString();
   }

};

int main()
{
   try
   {
      MyClass^ MyObject = gcnew MyClass;
      MethodInfo^ myMethodInfo;
      
      // Get the type of MyClass.
      Type^ myType = MyObject->GetType();
      
      // Get the method information for MyFunction.
      myMethodInfo = myType->GetMethod( "Myfunction" );
      
      // Get the parameters for Myfunction.
      array<ParameterInfo^>^myParameters = myMethodInfo->GetParameters();
      Console::WriteLine( "\nThe parameters of the method Myfunction of class
 MyClass are :\n" );
      
      // Display the position and type of the parameters.
      for ( int i = 0; i < myParameters->Length;
 i++ )
         Console::WriteLine( "The data type of parameter {0} is {1}.",
 myParameters[ i ]->Position + 1, myParameters[ i ]->ParameterType );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }

}

import System.*;
import System.Reflection.*;
import System.Security.*;

class MyClass
{
    public int myInt = 0;
    public String myString = null;

    public MyClass()
    {
    } //MyClass

    public void Myfunction(int
 i)
    {
    } //Myfunction
} //MyClass

class Type_GetMethod
{
    public static void main(String[]
 args)
    {
        try {
            MyClass myObject = new MyClass();
            MethodInfo myMethodInfo;
            // Get the type of MyClass.
            Type myType = myObject.GetType();
            // Get the method information for MyFunction. 
            myMethodInfo = myType.GetMethod("Myfunction");
            // Get the parameters for Myfunction.
            ParameterInfo myParameters[] = myMethodInfo.GetParameters();

            Console.WriteLine("\nThe parameters of the method Myfunction of"
                + " class MyClass are :\n");
            // Display the position and type of the parameters.
            for (int i = 0; i < myParameters.length;
 i++) {
                Console.WriteLine("The data type of parameter {0} is {1}."
,
                    (Int32)(myParameters[i].get_Position() + 1),
                    myParameters[i].get_ParameterType());
            }
        }
        catch (SecurityException e) {
            Console.WriteLine("SecurityException: " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: " + e.get_Message());
        }
    } //main
} //Type_GetMethod
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
MethodInfo
String クラス
DefaultBinder
ReflectionPermission
GetMethodImpl
GetMethods

Type.GetMethod メソッド

現在の Type の特定のメソッドを取得します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
Type.GetMethod (String) 指定した名前のパブリック メソッドを検索します。

.NET Compact Framework によってサポートされています。

Type.GetMethod (String, BindingFlags) 指定したバインディング制約を使用して、指定したメソッドを検索します。

.NET Compact Framework によってサポートされています。

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

.NET Compact Framework によってサポートされています。

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

.NET Compact Framework によってサポートされています。

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

.NET Compact Framework によってサポートされています。

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

.NET Compact Framework によってサポートされています。

参照参照

関連項目

Type クラス
Type メンバ
System 名前空間
MethodInfo
String クラス
DefaultBinder
ReflectionPermission
GetMethodImpl
GetMethods

Type.GetMethod メソッド (String, Type[], ParameterModifier[])

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

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

Public Function GetMethod ( _
    name As String, _
    types As Type(), _
    modifiers As ParameterModifier() _
) As MethodInfo
Dim instance As Type
Dim name As String
Dim types As Type()
Dim modifiers As ParameterModifier()
Dim returnValue As MethodInfo

returnValue = instance.GetMethod(name, types, modifiers)
public MethodInfo GetMethod (
    string name,
    Type[] types,
    ParameterModifier[] modifiers
)
public:
virtual MethodInfo^ GetMethod (
    String^ name, 
    array<Type^>^ types, 
    array<ParameterModifier>^ modifiers
) sealed
public final MethodInfo GetMethod (
    String name, 
    Type[] types, 
    ParameterModifier[] modifiers
)
public final function GetMethod (
    name : String, 
    types : Type[], 
    modifiers : ParameterModifier[]
) : MethodInfo

パラメータ

name

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

types

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

または

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

modifiers

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

戻り値
指定した要件と一致するパブリック メソッドが存在する場合は、そのパブリック メソッドを表す MethodInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

AmbiguousMatchException

指定した名前を持ち、指定したパラメータが設定されているメソッドが 2 つ以上存在します。

ArgumentNullException

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

または

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

または

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

ArgumentException

types が多次元です。

または

modifiers が多次元です。

または

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

解説解説

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

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

name の検索では大文字と小文字が区別されます。検索には、public static および public のインスタンス メソッドが含まれます。

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

メモメモ

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

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

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

メモメモ

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

.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
MethodInfo
String クラス
DefaultBinder
ParameterModifier
ReflectionPermission
GetMethodImpl
GetMethods

_Type.GetMethod メソッド

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

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

関連項目

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



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「Type.GetMethod」の関連用語

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

   

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



Type.GetMethodのページの著作権

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

©2026 GRAS Group, Inc.RSS