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-

若为 Default,则返回空数组。Default 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.

实现

示例

下面的示例创建一个具有两个公共方法和一个受保护方法的类,创建与 MyTypeClass对应的 Type 对象,获取所有公共和非公共方法,并显示它们的名称。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.Public中的一个。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()

返回为当前 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 NoNo NoNo
字段Field NoNo 可以。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 NoNo 可以。Yes. 方法(虚拟和非虚拟)可按名称隐藏或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套类型Nested Type NoNo NoNo
属性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 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类的资源库。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.

另请参阅

适用于