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

備註

針對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.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 和 unmanaged 呼叫慣例。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.

另請參閱

適用於