语言

Type.GetMethods 方法

定义

获取当前 Type方法。

重载

名称 说明
GetMethods(BindingFlags)

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

GetMethods()

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

GetMethods(BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

public:
 abstract cli::array <System::Reflection::MethodInfo ^> ^ GetMethods(System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public abstract System.Reflection.MethodInfo[] GetMethods(System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.MethodInfo[] GetMethods(System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
abstract member GetMethods : System.Reflection.BindingFlags -> System.Reflection.MethodInfo[]
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 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.Instance,BindingFlags.Static以及至少一个和BindingFlags.NonPublic一个BindingFlags.Public。

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

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

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

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

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

  • 指定 BindingFlags.FlattenHierarchy 为包含 public 层次结构中的静态成员和 protected 静态成员; private 不包括继承类中的静态成员。

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

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

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

有关详细信息,请参阅 System.Reflection.BindingFlags。

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

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

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

另请参阅

适用于

GetMethods()

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

public:
 cli::array <System::Reflection::MethodInfo ^> ^ GetMethods();
public:
 virtual cli::array <System::Reflection::MethodInfo ^> ^ GetMethods();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo[] GetMethods();
public System.Reflection.MethodInfo[] GetMethods();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethods : unit -> System.Reflection.MethodInfo[]
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 方法在反射类型时返回的基类成员。

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

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

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

注释

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

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

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

另请参阅

适用于