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

namenull 参照 (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

namenull 参照 (Visual Basic では Nothing) です。

または

typesnull 参照 (Visual Basic では Nothing) です。

または

types1 つ要素null 参照 (Visual Basic では Nothing) です。

ArgumentException

types多次元です。

または

modifiers多次元です。

または

typesmodifiers長さ異なります

解説解説

既定バインダ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>" というメンバ検索します

使用例使用例

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

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

namenull 参照 (Visual Basic では Nothing) です。

または

typesnull 参照 (Visual Basic では Nothing) です。

または

types1 つ要素null 参照 (Visual Basic では Nothing) です。

ArgumentException

types多次元です。

または

modifiers多次元です。

または

typesmodifiers長さ異なります

解説解説

既定バインダ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

namenull 参照 (Visual Basic では Nothing) です。

または

typesnull 参照 (Visual Basic では Nothing) です。

または

types1 つ要素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

namenull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

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

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

namenull 参照 (Visual Basic では Nothing) です。

または

typesnull 参照 (Visual Basic では Nothing) です。

または

types1 つ要素null 参照 (Visual Basic では Nothing) です。

ArgumentException

types多次元です。

または

modifiers多次元です。

または

typesmodifiers長さ異なります

解説解説

既定バインダ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 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Type.GetMethod」の関連用語

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

   

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



Type.GetMethodのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS