Type.GetMethods 方法

定義

取得目前 Type 的方法。Gets the methods of the current Type.

多載

GetMethods(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的方法。When overridden in a derived class, searches for the methods defined for the current Type, using the specified binding constraints.

GetMethods()

傳回目前 Type 的所有公用方法。Returns all the public methods of the current Type.

GetMethods(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的方法。When overridden in a derived class, searches for the methods defined for the current Type, using the specified binding constraints.

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

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or-

要傳回空陣列的 DefaultDefault to return an empty array.

傳回

MethodInfo[]

MethodInfo 物件的陣列,代表為目前 Type 定義之符合指定繫結條件約束的所有方法。An array of MethodInfo objects representing all methods defined for the current Type that match the specified binding constraints.

-或--or- 如果沒有任何方法定義給目前的 MethodInfo,或沒有已定義的方法符合繫結條件約束,則為 Type 類型的空陣列。An empty array of type MethodInfo, if no methods are defined for the current Type, or if none of the defined methods match the binding constraints.

實作

範例

下列範例會建立具有兩個公用方法和一個受保護方法的類別、建立 Type 對應的物件 MyTypeClass 、取得所有公用和非公用方法,並顯示其名稱。The following example creates a class with two public methods and one protected method, creates a Type object corresponding to MyTypeClass, gets all public and non-public methods, and displays their names.

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.PublicFor the GetMethods(BindingFlags) overload to successfully retrieve method information, the bindingAttr argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public.

下列 BindingFlags 篩選旗標可以用來定義要包含在搜尋中的方法:The following BindingFlags filter flags can be used to define which methods to include in the search:

  • 指定 BindingFlags.Instance 要包含實例方法。Specify BindingFlags.Instance to include instance methods.

  • 指定 BindingFlags.Static 要包含靜態方法。Specify BindingFlags.Static to include static methods.

  • 指定 BindingFlags.Public 要在搜尋中包含公用方法。Specify BindingFlags.Public to include public methods in the search.

  • 指定 BindingFlags.NonPublic 要包含非公用方法 (也就是在搜尋中) 私用、內部和受保護的方法。Specify BindingFlags.NonPublic to include non-public methods (that is, private, internal, and protected methods) in the search. 只會傳回基類上受保護和內部的方法;不會傳回基底類別上的私用方法。Only protected and internal methods on base classes are returned; private methods on base classes are not returned.

  • 指定 BindingFlags.FlattenHierarchypublic 在階層中包含和 protected 靜態成員; private 不包含繼承類別中的靜態成員。Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

  • BindingFlags.Default單獨指定以傳回空 MethodInfo 陣列。Specify BindingFlags.Default alone to return an empty MethodInfo array.

BindingFlags 可以使用下列修飾詞旗標來變更搜尋的運作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly 只搜尋在上宣告的方法 Type ,而不是單純繼承的方法。BindingFlags.DeclaredOnly to search only the methods declared on the Type, not methods that were simply inherited.

如需相關資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

GetMethods方法不會以特定順序傳回方法,例如字母順序或宣告順序。The GetMethods method does not return methods in a particular order, such as alphabetical or declaration order. 您的程式碼不得相依于傳回方法的順序,因為該順序會有所不同。Your code must not depend on the order in which methods are returned, because that order varies.

如果目前的 Type 代表結構化泛型型別,這個方法會傳回 MethodInfo 類型參數取代為適當類型引數的物件。If the current Type represents a constructed generic type, this method returns the MethodInfo objects with the type parameters replaced by the appropriate type arguments.

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的方法, Object 如果沒有類別條件約束,則會搜尋的方法。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.

另請參閱

適用於

GetMethods()

傳回目前 Type 的所有公用方法。Returns all the public methods of the current 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[]

MethodInfo 物件的陣列,代表為目前 Type 定義的所有公用方法。An array of MethodInfo objects representing all the public methods defined for the current Type.

-或--or- MethodInfo 類型的空陣列 (如果沒有為目前 Type 定義公用方法)。An empty array of type MethodInfo, if no public methods are defined for the current Type.

實作

備註

GetMethods方法不會以特定順序傳回方法,例如字母順序或宣告順序。The GetMethods method does not return methods in a particular order, such as alphabetical or declaration order. 您的程式碼不得相依于傳回方法的順序,因為該順序會有所不同。Your code must not depend on the order in which methods are returned, because that order varies.

此呼叫所傳回的方法陣列中不包含函式。Constructors are not included in the array of methods returned by this call. 進行個別的呼叫, GetConstructors() 以取得方法的函式。Make a separate call to GetConstructors() to get the constructor methods.

下表顯示在反映型別時,方法會傳回基類的成員 GetThe following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位一律會依名稱和簽章進行隱藏。A field is always hide-by-name-and-signature.
事件Event 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為隱藏名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的附注2。See note 2 below.
方法Method No 可以。Yes. (虛擬和非虛擬) 的方法都可以依名稱或依名稱、依名稱和簽章來隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀型別Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為隱藏名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的附注2。See note 2 below.
  1. 依名稱和簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、個 sentinel 和非受控呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 針對反映,屬性和事件會依名稱和簽章來隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不是一般型別系統的一部分。Custom attributes are not part of the common type system.

注意

查閱函式和方法時,您不能省略參數。You cannot omit parameters when looking up constructors and methods. 叫用時,您只能省略參數。You can only omit parameters when invoking.

如果目前的 Type 代表結構化泛型型別,這個方法會傳回 MethodInfo 類型參數取代為適當類型引數的物件。If the current Type represents a constructed generic type, this method returns the MethodInfo objects with the type parameters replaced by the appropriate type arguments.

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的方法, Object 如果沒有類別條件約束,則會搜尋的方法。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.

另請參閱

適用於