System.Type.GetMethod 方法 (String, BindingFlags, Binder, Type[], ParameterModifier[])

方法描述

使用指定绑定约束,搜索其参数与指定参数类型及修饰符匹配的指定方法。

语法定义(C# System.Type.GetMethod 方法 (String, BindingFlags, Binder, Type[], ParameterModifier[]) 的用法)

public MethodInfo GetMethod(
	string name,
	BindingFlags bindingAttr,
	Binder binder,
	Type[] types,
	ParameterModifier[] modifiers
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
name System-String 包含要获取的方法名称的字符串。
bindingAttr System-Reflection-BindingFlags 一个位屏蔽,由一个或多个指定搜索执行方式的 BindingFlags 组成。- 或 -零,以返回 null。
binder System-Reflection-Binder 一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。- 或 -空引用(Visual Basic 中为 Nothing)将使用 DefaultBinder。
types System-Type[] 表示此方法要获取的参数的个数、顺序和类型的 Type 对象数组。- 或 -空的 Type 对象数组(由 EmptyTypes 字段提供),用来获取不采用参数的方法。
modifiers System-Reflection-ParameterModifier[] ParameterModifier 对象数组,表示与 types 数组中的相应元素关联的特性。仅当通过 COM 互操作进行调用时才使用,而且仅处理通过引用传递的参数。默认的联编程序不处理此参数。
返回值 System.Reflection.MethodInfo 表示符合指定要求的方法的对象(如果找到的话);否则为 null。

提示和注释

虽然默认联编程序不处理 ParameterModifier(modifiers 参数),但您可以使用抽象 System.Reflection.Binder 类编写确实处理 modifiers 的自定义联编程序。 ParameterModifier 仅当通过 COM 互操作进行调用时才使用,而且仅处理通过引用传递的参数。

可以使用下列 BindingFlags 筛选标志定义包含在搜索中的方法:

为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static。

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

指定 BindingFlags.NonPublic 可以在搜索中包括非公共方法(即私有方法、内部方法和受保护方法)。

指定 BindingFlags.FlattenHierarchy 以便沿层次结构向上包括 public 和 protected 静态成员;不包括继承类中的 private 静态成员。

下列 BindingFlags 修饰符标志可用于更改搜索的执行方式:

BindingFlags.IgnoreCase ,表示忽略 name 的大小写。

BindingFlags.DeclaredOnly ,表示仅搜索在 Type 上声明的方法,而不搜索简单继承的方法。

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

注意

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

如果当前 T:System.Type 表示某种已构造的泛型类型,则此方法将返回 MethodInfo,并且其类型参数由相应的类型参数替换。

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

注意

对于泛型方法,请不要在 name 中包括此类型参数。 例如,C# 代码 GetMember("MyMethod") 搜索文本名称为“MyMethod”的成员,而不是搜索一个名为 MyMethod 的方法,该方法具有一个 int 类型的泛型参数。

System.Type.GetMethod 方法 (String, BindingFlags, Binder, Type[], ParameterModifier[])例子

Visual C# 2005 示例需要 /unsafe 编译器选项。

using System;
using System.Reflection;

class Program
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }


    public void MethodA(ref int r) {}

    // Method that takes an out parameter.
    public void MethodA(int i, out int o) { o = 100; }


  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

  }
}

异常

异常 异常描述
AmbiguousMatchException 找到多个有指定名称且与指定绑定约束匹配的方法。
ArgumentNullException
  • name 为 null。
  • types 为 null。
  • types 中的一个元素为 null。
ArgumentException
  • types 是多维的。
  • modifiers 是多维的。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。