Type.GetMethods 方法

定義

取得目前 Type 的方法。

多載

GetMethods(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的方法。

GetMethods()

傳回目前 Type 的所有公用方法。

GetMethods(BindingFlags)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的方法。

public:
 abstract cli::array <System::Reflection::MethodInfo ^> ^ GetMethods(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.MethodInfo[] GetMethods (System.Reflection.BindingFlags bindingAttr);
abstract member GetMethods : System.Reflection.BindingFlags -> System.Reflection.MethodInfo[]
Public MustOverride Function GetMethods (bindingAttr As BindingFlags) As MethodInfo()

參數

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回空陣列的 Default

傳回

MethodInfo 物件的陣列,代表為目前 Type 定義之符合指定繫結條件約束的所有方法。

-或-

如果沒有任何方法定義給目前的 MethodInfo,或沒有已定義的方法符合繫結條件約束,則為 Type 類型的空陣列。

實作

範例

下列範例會建立具有兩個公用方法和一個受保護方法的類別、建立 Type 對應至 MyTypeClass 的物件、取得所有公用和非公用方法,並顯示其名稱。

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

// Create a class having two public methods and one protected method.
public ref class MyTypeClass
{
public:
   void MyMethods(){}

   int MyMethods1()
   {
      return 3;
   }


protected:
   String^ MyMethods2()
   {
      return "hello";
   }
};

void DisplayMethodInfo( array<MethodInfo^>^myArrayMethodInfo )
{
   // Display information for all methods.
   for ( int i = 0; i < myArrayMethodInfo->Length; i++ )
   {
      MethodInfo^ myMethodInfo = dynamic_cast<MethodInfo^>(myArrayMethodInfo[ i ]);
      Console::WriteLine( "\nThe name of the method is {0}.", myMethodInfo->Name );
   }
}

int main()
{
   Type^ myType = MyTypeClass::typeid;
   
   // Get the public methods.
   array<MethodInfo^>^myArrayMethodInfo = myType->GetMethods( static_cast<BindingFlags>(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.
   array<MethodInfo^>^myArrayMethodInfo1 = myType->GetMethods( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance | BindingFlags::DeclaredOnly) );
   Console::WriteLine( "\nThe number of protected methods is {0}->", myArrayMethodInfo1->Length );
   
   // Display information for all methods.
   DisplayMethodInfo( myArrayMethodInfo1 );
}
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);
        }
    }
}

Imports System.Reflection
Imports System.Reflection.Emit

' Create a class having two public methods and one protected method.
Public Class MyTypeClass
    Public Sub MyMethods()
    End Sub
    Public Function MyMethods1() As Integer
        Return 3
    End Function 'MyMethods1
    Protected Function MyMethods2() As [String]
        Return "hello"
    End Function 'MyMethods2
End Class
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

    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
End Class

備註

GetMethods(BindingFlags)若要讓多載成功擷取方法資訊,自 bindingAttr 變數必須包含至少一個 BindingFlags.InstanceBindingFlags.Static ,以及至少一個 BindingFlags.NonPublicBindingFlags.Public

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的方法:

  • 指定 BindingFlags.Instance 以包含實例方法。

  • 指定 BindingFlags.Static 以包含靜態方法。

  • 指定 BindingFlags.Public 以在搜尋中包含公用方法。

  • 指定 BindingFlags.NonPublic 以在搜尋中包含非公用方法 (,也就是私人、內部和受保護的方法) 。 只會傳回基類上的受保護和內部方法;不會傳回基類上的私用方法。

  • 指定要 BindingFlags.FlattenHierarchy 在階層中加入 publicprotected 靜態成員; private 繼承類別中的靜態成員不包含在內。

  • 單獨指定 BindingFlags.Default 以傳回空 MethodInfo 陣列。

下列 BindingFlags 修飾詞旗標可用來變更搜尋的運作方式:

  • BindingFlags.DeclaredOnly 只搜尋 在 上 Type 宣告的方法,而不是只繼承的方法。

如需相關資訊,請參閱 System.Reflection.BindingFlags

在 .NET 6 和舊版中 GetMethods ,方法不會以特定順序傳回方法,例如字母順序或宣告順序。 您的程式碼不得依存于傳回方法的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的中繼資料排序來確定。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo 物件,其類型參數會由適當的型別引數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的方法,如果沒有任何類別條件約束,則為 的方法 Object

另請參閱

適用於

GetMethods()

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

傳回目前 Type 的所有公用方法。

public:
 cli::array <System::Reflection::MethodInfo ^> ^ GetMethods();
public:
 virtual cli::array <System::Reflection::MethodInfo ^> ^ GetMethods();
public System.Reflection.MethodInfo[] GetMethods ();
member this.GetMethods : unit -> System.Reflection.MethodInfo[]
abstract member GetMethods : unit -> System.Reflection.MethodInfo[]
override this.GetMethods : unit -> System.Reflection.MethodInfo[]
Public Function GetMethods () As MethodInfo()

傳回

MethodInfo 物件的陣列,代表為目前 Type 定義的所有公用方法。

-或-

MethodInfo 類型的空陣列 (如果沒有為目前 Type 定義公用方法)。

實作

備註

在 .NET 6 和舊版中 GetMethods ,方法不會以特定順序傳回方法,例如字母順序或宣告順序。 您的程式碼不得依存于傳回方法的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的中繼資料排序來確定。

建構函式不會包含在這個呼叫所傳回的方法陣列中。 對 進行個別呼叫 GetConstructors() 以取得建構函式方法。

下表顯示反映類型時,方法會傳 Get 回基類的成員。

成員類型 Static 非靜態
建構函式
欄位 可以。 欄位一律會依名稱與簽章隱藏。
事件 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附注 2。
方法 可以。 (虛擬和非虛擬) 的方法可以是依名稱隱藏或依名稱隱藏和簽章。
巢狀類型
屬性 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附注 2。
  1. 隱藏名稱與簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回型別、參數類型、sentinels 和 Unmanaged 呼叫慣例。 這是二進位比較。

  2. 對於反映,屬性和事件會依名稱與簽章隱藏。 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。

  3. 自訂屬性不是一般型別系統的一部分。

注意

查閱建構函式和方法時,您無法省略參數。 您只能在叫用時省略參數。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo 物件,其類型參數會由適當的型別引數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的方法,如果沒有任何類別條件約束,則為 的方法 Object

另請參閱

適用於