Type.GetMethods Type.GetMethods Type.GetMethods Type.GetMethods Method

定義

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

多載

GetMethods(BindingFlags) GetMethods(BindingFlags) GetMethods(BindingFlags) GetMethods(BindingFlags)

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

GetMethods() GetMethods() GetMethods() GetMethods()

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

GetMethods(BindingFlags) GetMethods(BindingFlags) GetMethods(BindingFlags) 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 BindingFlags BindingFlags BindingFlags

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

-或--or-

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

傳回

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 '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	

備註

BindingFlags.Static BindingFlags.Instance bindingAttr BindingFlags.Public BindingFlags.NonPublic若要讓多載成功取得方法資訊,引數必須包含至少一個和,以及至少一個和。 GetMethods(BindingFlags)For 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.FlattenHierarchy在階層publicprotected包含和靜態成員;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() GetMethods() GetMethods() GetMethods()

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

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

下表顯示當在類型上反映時, Get方法所傳回之基類的成員。The 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 - 事件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. [逐名稱] 和 [簽章] 會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、sentinels 和非受控呼叫慣例。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.

另請參閱

適用於