Type.GetMethods 方法

定义

获取当前 Type 的方法。

重载

GetMethods(BindingFlags)

当在派生类中重写时,使用指定绑定约束,搜索为当前 Type 定义的方法。

GetMethods()

返回为当前 Type 的所有公共方法。

GetMethods(BindingFlags)

当在派生类中重写时,使用指定绑定约束,搜索为当前 Type 定义的方法。

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

枚举值的按位组合,这些值指定如何进行搜索。

若为 Default,则返回空数组。

返回

表示为当前 MethodInfo 定义的匹配指定绑定约束的所有方法的 Type 对象数组。

如果没有为当前 MethodInfo 定义的方法,或者如果没有一个定义的方法匹配绑定约束,则为 Type 类型的空数组。

实现

示例

以下示例创建一个具有两个公共方法和一个受保护方法的类,创建 Type 对应于 MyTypeClass的对象,获取所有公共和非公共方法,并显示其名称。

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.StaticBindingFlags.Instance一个 ,以及至少一个 BindingFlags.NonPublicBindingFlags.Public

以下 BindingFlags 筛选器标志可用于定义要在搜索中包括的方法:

  • 指定 BindingFlags.Instance 以包括实例方法。

  • 指定 BindingFlags.Static 以包括静态方法。

  • 指定 BindingFlags.Public 以在搜索中包含公共方法。

  • 指定 BindingFlags.NonPublic 以包括非公共方法 (即搜索中) 私有、内部和受保护方法。 仅返回基类上的受保护和内部方法;不返回基类上的私有方法。

  • 指定 BindingFlags.FlattenHierarchy 以在层次结构中包括 publicprotected 静态成员; private 不包括继承类中的静态成员。

  • 单独指定 BindingFlags.Default 以返回空 MethodInfo 数组。

以下 BindingFlags 修饰符标志可用于更改搜索的工作方式:

  • BindingFlags.DeclaredOnly 如果仅搜索 上 Type声明的方法,则只搜索只是继承的方法。

有关更多信息,请参见System.Reflection.BindingFlags

在 .NET 6 及更早版本中, GetMethods 方法不按特定顺序(如字母顺序或声明顺序)返回方法。 代码不得依赖于方法的返回顺序,因为该方法的顺序会有所不同。 但是,从 .NET 7 开始,排序基于程序集中的元数据排序是确定性的。

如果当前 Type 表示构造的泛型类型,则此方法返回 MethodInfo 由相应类型参数替换的类型参数的对象。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的方法,如果没有类约束,则搜索 的方法 Object

另请参阅

适用于

GetMethods()

返回为当前 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 定义的所有公共方法的 Type 对象数组。

如果没有为当前 MethodInfo 定义的公共方法,则为 Type 类型的空数组。

实现

注解

在 .NET 6 及更早版本中, GetMethods 方法不按特定顺序(如字母顺序或声明顺序)返回方法。 代码不得依赖于方法的返回顺序,因为该方法的顺序会有所不同。 但是,从 .NET 7 开始,排序基于程序集中的元数据排序是确定性的。

构造函数不包括在此调用返回的方法数组中。 单独调用 以获取 GetConstructors() 构造函数方法。

下表显示了在对类型进行反射时,方法返回 Get 的基类成员。

成员类型 静态 非静态
构造函数
字段 可以。 字段始终按名称和签名隐藏。
事件 不适用 常见类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的注释 2。
方法 可以。 (虚拟和非虚拟) 的方法可以是按名称隐藏或按名称和签名隐藏。
嵌套类型
properties 不适用 常见类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的注释 2。
  1. 按名称和签名隐藏考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinels 和非托管调用约定。 这是二进制比较。

  2. 对于反射,属性和事件按名称和签名隐藏。 如果在基类中具有同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且您将无法访问基类上的 setter。

  3. 自定义属性不是通用类型系统的一部分。

注意

在查找构造函数和方法时,不能省略参数。 只能在调用时省略参数。

如果当前 Type 表示构造的泛型类型,则此方法返回 MethodInfo 由相应类型参数替换的类型参数的对象。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的方法,如果没有类约束,则搜索 的方法 Object

另请参阅

适用于