次の方法で共有


Type.GetMethods メソッド

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

オーバーロードの一覧

現在の Type のすべてのパブリック メソッドを返します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function GetMethods() As MethodInfo()

[C#] public MethodInfo[] GetMethods();

[C++] public: MethodInfo* GetMethods() [];

[JScript] public function GetMethods() : MethodInfo[];

派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type に対して定義されているメソッドを検索します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public MustOverride Function GetMethods(BindingFlags) As MethodInfo() Implements IReflect.GetMethods

[C#] public abstract MethodInfo[] GetMethods(BindingFlags);

[C++] public: virtual MethodInfo* GetMethods(BindingFlags) [] = 0;

[JScript] public abstract function GetMethods(BindingFlags) : MethodInfo[];

使用例

[Visual Basic, C#, C++] 2 つのパブリック メソッドと 1 つのプロテクト メソッドを持つクラスを作成し、 MyTypeClass に対応する Type オブジェクトを作成し、パブリックおよび非パブリックのすべてのメソッドを取得して、それらの数と名前を表示する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、GetMethods のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

' Create a class having two public methods and one protected method.
Public Class MyTypeClass
    Public Sub MyMethods()
    End Sub 'MyMethods
    Public Function MyMethods1() As Integer
        Return 3
    End Function 'MyMethods1
    Protected Function MyMethods2() As [String]
        Return "hello"
    End Function 'MyMethods2
End Class 'MyTypeClass
Public Class TypeMain
    Public Shared Sub Main()

        Dim myType As Type = GetType(MyTypeClass)
        ' Get the public methods.
        Dim myArrayMethodInfo As MethodInfo() = myType.GetMethods((BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
        Console.WriteLine((ControlChars.Cr + "The number of public methods is " & myArrayMethodInfo.Length.ToString() & "."))
        ' Display all the public methods.
        DisplayMethodInfo(myArrayMethodInfo)
        ' Get the nonpublic methods.
        Dim myArrayMethodInfo1 As MethodInfo() = myType.GetMethods((BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
        Console.WriteLine((ControlChars.Cr + "The number of protected methods is " & myArrayMethodInfo1.Length.ToString() & "."))
        ' Display all the nonpublic methods.
        DisplayMethodInfo(myArrayMethodInfo1)
    End Sub 'Main

    Public Shared Sub DisplayMethodInfo(ByVal myArrayMethodInfo() As MethodInfo)
        ' Display information for all methods.
        Dim i As Integer
        For i = 0 To myArrayMethodInfo.Length - 1
            Dim myMethodInfo As MethodInfo = CType(myArrayMethodInfo(i), MethodInfo)
            Console.WriteLine((ControlChars.Cr + "The name of the method is " & myMethodInfo.Name & "."))
        Next i
    End Sub 'DisplayMethodInfo
End Class 'TypeMain    

[C#] 

using System;
using System.Reflection;
using System.Reflection.Emit;

        // Create a class having two public methods and one protected method.
public class MyTypeClass
{
    public void MyMethods()
    {
    }
    public int MyMethods1() 
    {
        return 3;
    }
    protected String MyMethods2()
    {
        return "hello";
    }
}
public class TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public methods.
        MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of public methods is {0}.", myArrayMethodInfo.Length);
        // Display all the methods.
        DisplayMethodInfo(myArrayMethodInfo);
        // Get the nonpublic methods.
        MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of protected methods is {0}.", myArrayMethodInfo1.Length);
        // Display information for all methods.
        DisplayMethodInfo(myArrayMethodInfo1);        
    }
    public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo)
    {
        // Display information for all methods.
        for(int i=0;i<myArrayMethodInfo.Length;i++)
        {
            MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
            Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.Name);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class having two public methods and one protected method.
public __gc class MyTypeClass {
public:
   void MyMethods() {
   }
   int MyMethods1() {
      return 3;
   }
protected:
   String* MyMethods2() {
      return S"hello";
   }
};


void DisplayMethodInfo(MethodInfo* myArrayMethodInfo[]) {
   // Display information for all methods.
   for (int i=0;i<myArrayMethodInfo->Length;i++) {
      MethodInfo*  myMethodInfo = dynamic_cast<MethodInfo*>(myArrayMethodInfo[i]);
      Console::WriteLine(S"\nThe name of the method is {0}.", myMethodInfo->Name);
   }
}
int main() {
   Type* myType =(__typeof(MyTypeClass));
   // Get the public methods.
   MethodInfo*  myArrayMethodInfo[] = myType->GetMethods(static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance|BindingFlags::DeclaredOnly));
   Console::WriteLine(S"\nThe number of public methods is {0}->",__box( myArrayMethodInfo->Length));
   // Display all the methods.
   DisplayMethodInfo(myArrayMethodInfo);
   // Get the nonpublic methods.
   MethodInfo*  myArrayMethodInfo1[] = myType->GetMethods(static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance|BindingFlags::DeclaredOnly));
   Console::WriteLine(S"\nThe number of protected methods is {0}->",__box( myArrayMethodInfo1->Length));
   // Display information for all methods.
   DisplayMethodInfo(myArrayMethodInfo1);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Type クラス | Type メンバ | System 名前空間