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-

若为 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.

实现

示例

下面的示例使用两个公共方法和一个受保护的方法创建一个类,创建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 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
PropertyProperty 不适用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. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。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.

另请参阅

适用于