通过


Type.GetMethod 方法

定义

获取当前 Type方法的特定方法。

重载

名称 说明
GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

使用指定的绑定约束和指定的调用约定搜索其参数与指定的泛型参数计数、参数类型和修饰符匹配的指定方法。

GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

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

GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])

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

GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

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

GetMethod(String, Int32, Type[], ParameterModifier[])

搜索与指定的泛型参数计数、参数类型和修饰符匹配的指定公共方法。

GetMethod(String, Int32, BindingFlags, Type[])

使用指定的绑定约束搜索其参数与指定的泛型参数计数和参数类型匹配的指定方法。

GetMethod(String, BindingFlags)

使用指定的绑定约束搜索指定的方法。

GetMethod(String, BindingFlags, Type[])

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

GetMethod(String, Int32, Type[])

搜索与指定的泛型参数计数和参数类型匹配的指定公共方法。

GetMethod(String, Type[])

搜索其参数与指定参数类型匹配的指定公共方法。

GetMethod(String)

搜索具有指定名称的公共方法。

GetMethod(String, Type[], ParameterModifier[])

搜索其参数与指定参数类型和修饰符匹配的指定公共方法。

GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

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

使用指定的绑定约束和指定的调用约定搜索其参数与指定的泛型参数计数、参数类型和修饰符匹配的指定方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo

参数

name
String

包含要获取的公共方法的名称的字符串。

genericParameterCount
Int32

方法的泛型类型参数数。

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。

-或-

Default null返回 。

binder
Binder

一个对象,定义一组属性并启用绑定,这可能涉及选择重载的方法、强制参数类型以及通过反射调用成员。

-或-

使用 的 null 引用(Nothing 在 Visual Basic DefaultBinder中)。

callConvention
CallingConventions

指定用于参数顺序和布局的规则集、传递返回值的方式、参数的寄存器以及如何清理堆栈的对象。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

modifiers
ParameterModifier[]

表示与数组中types相应元素关联的属性的对象数组ParameterModifier。 仅在通过 COM 互操作调用时使用,并且仅处理通过引用传递的参数。 默认绑定器不处理此参数。

返回

一个对象,表示与指定的泛型参数计数、参数类型、修饰符、绑定约束和调用约定匹配的方法(如果找到);否则,为 null.

属性

例外

namenull

-或-

typesnull

-或-

数组中的 types 元素之一是 null

genericParameterCount 为负数。

适用于

GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

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

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

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo

参数

name
String

包含要获取的方法的名称的字符串。

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。

-或-

Default null返回 。

binder
Binder

一个对象,定义一组属性并启用绑定,这可能涉及选择重载的方法、强制参数类型以及通过反射调用成员。

-或-

使用 的 null 引用(Nothing 在 Visual Basic DefaultBinder中)。

callConvention
CallingConventions

指定用于参数顺序和布局的规则集、传递返回值的方式、参数的寄存器以及如何清理堆栈的对象。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

modifiers
ParameterModifier[]

表示与数组中types相应元素关联的属性的对象数组ParameterModifier。 仅在通过 COM 互操作调用时使用,并且仅处理通过引用传递的参数。 默认绑定器不处理此参数。

返回

一个对象,表示符合指定要求的方法(如果找到);否则,为 null.

实现

属性

例外

使用指定的名称找到多个方法,并匹配指定的绑定约束。

namenull

-或-

typesnull

-或-

其中 types 一个元素是 null

types 是多维。

-或-

modifiers 是多维。

示例

以下示例查找特定重载 MethodA、指定绑定约束、调用约定和各种参数类型。

注释

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


using System;
using System.Reflection;

class Program3
{
    // 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,
        CallingConventions.Any,
        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,
        CallingConventions.Any,
        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,
        CallingConventions.Any,
        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,
        CallingConventions.Any,
        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,
        CallingConventions.Any,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
open System.Reflection

type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // Get MethodA(int i, int j)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int[] i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int[]> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int* i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakePointerType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(ref int r)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int i, out int o)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:
    
    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an integer and an out parameter. Note that an
    ' Imports reference is needed to System.Runtime.InteropServices
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(ByVal i As Integer, ByVal l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            CallingConventions.Any, _
            New Type() {GetType(System.Int32), _
            GetType(System.Int64)}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get  MethodA(ByVal i() As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            CallingConventions.Any, _
            New Type() {GetType(System.Int32())}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
        BindingFlags.Public Or BindingFlags.Instance, _
        Nothing, _
        CallingConventions.Any, _
        New Type() {GetType(System.Int32).MakeByRefType}, _
        Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
        BindingFlags.Public Or BindingFlags.Instance, _
        Nothing, _
        CallingConventions.Any, _
        New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
        Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注解

尽管默认联编程序不处理 ParameterModifiermodifiers 参数),但可以使用抽象 System.Reflection.Binder 类编写一个处理自定义 modifiers联编程序。 ParameterModifier 仅在通过 COM 互作调用时使用,并且仅处理通过引用传递的参数。

下表显示了通过 GetXXX 方法在反射类型时返回的基类成员。

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

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

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

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

  • 您必须指定BindingFlags.InstanceBindingFlags.Static才能获得返回结果。

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

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

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

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

  • BindingFlags.IgnoreCase 用于忽略 name 的大小写。

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

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

注释

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

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

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

注释

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

另请参阅

适用于

GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])

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

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

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo

参数

name
String

包含要获取的公共方法的名称的字符串。

genericParameterCount
Int32

方法的泛型类型参数数。

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。

-或-

Default null返回 。

binder
Binder

一个对象,定义一组属性并启用绑定,这可能涉及选择重载的方法、强制参数类型以及通过反射调用成员。

-或-

使用 的 null 引用(Nothing 在 Visual Basic DefaultBinder中)。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

modifiers
ParameterModifier[]

表示与数组中types相应元素关联的属性的对象数组ParameterModifier。 仅在通过 COM 互操作调用时使用,并且仅处理通过引用传递的参数。 默认绑定器不处理此参数。

返回

一个对象,表示与指定的泛型参数计数、参数类型、修饰符和绑定约束匹配的方法(如果找到);否则,为 null.

属性

例外

namenull

-或-

typesnull

-或-

数组中的 types 元素之一是 null

genericParameterCount 为负数。

适用于

GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

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

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

public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo

参数

name
String

包含要获取的方法的名称的字符串。

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。

-或-

Default null返回 。

binder
Binder

一个对象,定义一组属性并启用绑定,这可能涉及选择重载的方法、强制参数类型以及通过反射调用成员。

-或-

使用 的 null 引用(Nothing 在 Visual Basic DefaultBinder中)。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

modifiers
ParameterModifier[]

表示与数组中types相应元素关联的属性的对象数组ParameterModifier。 仅在通过 COM 互操作调用时使用,并且仅处理通过引用传递的参数。 默认绑定器不处理此参数。

返回

一个对象,表示符合指定要求的方法(如果找到);否则,为 null.

实现

属性

例外

使用指定的名称找到多个方法,并匹配指定的绑定约束。

namenull

-或-

typesnull

-或-

其中 types 一个元素是 null

types 是多维。

-或-

modifiers 是多维。

示例

以下示例查找特定重载 MethodA,指定绑定约束和各种参数类型。

注释

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


using System;
using System.Reflection;

class Program5
{
    // 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);
  }
}
open System
open System.Reflection

type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // Get MethodA(int i, int j)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int[] i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int[]> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int* i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakePointerType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(ref int r)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int i, out int o)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:
    
    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an out parameter. Note that an Imports
    ' reference is needed to System.Runtime.InteropServices 
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(ByVal i As Integer, ByVal l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32), _
            GetType(System.Int64)}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i() As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32())}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32).MakeByRefType}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注解

尽管默认联编程序不处理 ParameterModifiermodifiers 参数),但可以使用抽象 System.Reflection.Binder 类编写一个处理自定义 modifiers联编程序。 ParameterModifier 仅在通过 COM 互作调用时使用,并且仅处理通过引用传递的参数。

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

  • 您必须指定BindingFlags.InstanceBindingFlags.Static才能获得返回结果。

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

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

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

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

  • BindingFlags.IgnoreCase 用于忽略 name 的大小写。

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

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

注释

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

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

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

注释

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

另请参阅

适用于

GetMethod(String, Int32, Type[], ParameterModifier[])

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

搜索与指定的泛型参数计数、参数类型和修饰符匹配的指定公共方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type(), modifiers As ParameterModifier()) As MethodInfo

参数

name
String

包含要获取的公共方法的名称的字符串。

genericParameterCount
Int32

方法的泛型类型参数数。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

modifiers
ParameterModifier[]

表示与数组中types相应元素关联的属性的对象数组ParameterModifier。 仅在通过 COM 互操作调用时使用,并且仅处理通过引用传递的参数。 默认绑定器不处理此参数。

返回

一个对象,表示与指定的泛型参数计数、参数类型和修饰符匹配的公共方法(如果找到);否则,为 null.

属性

例外

namenull

-或-

typesnull

-或-

数组中的 types 元素之一是 null

genericParameterCount 为负数。

适用于

GetMethod(String, Int32, BindingFlags, Type[])

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

使用指定的绑定约束搜索其参数与指定的泛型参数计数和参数类型匹配的指定方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, types As Type()) As MethodInfo

参数

name
String

包含要获取的方法的名称的字符串。

genericParameterCount
Int32

方法的泛型类型参数数。

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。

-或-

Default null返回 。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

返回

一个对象,表示与指定的泛型参数计数、参数类型和绑定约束匹配的方法(如果找到);否则,为 null.

属性

适用于

GetMethod(String, BindingFlags)

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

使用指定的绑定约束搜索指定的方法。

public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.MethodInfo GetMethod(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
abstract member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags) As MethodInfo

参数

name
String

包含要获取的方法的名称的字符串。

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。

-或-

Default null返回 。

返回

一个对象,表示符合指定要求的方法(如果找到);否则,为 null.

实现

属性

例外

使用指定的名称找到多个方法,并匹配指定的绑定约束。

namenull

示例

以下示例获取与指定绑定标志匹配的方法。

using System;
using System.Reflection;

class Program2
{
    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {
        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA",
            BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine("Found method: {0}", mInfo);
    }
}
open System.Reflection

type Program() =
    // Method to get:
    member _.MethodA() = ()

// Get MethodA()
let mInfo = typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance)
printfn $"Found method: {mInfo}"

Imports System.Reflection

Class Program

    ' Method to get:
    Public Sub MethodA()
    End Sub


    Public Shared Sub Main(ByVal args() As String)

        ' Get MethodA()
        Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注解

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

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

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

如果方法重载且多个重载满足参数指定的 bindingAttr 约束,该方法将引发异常 AmbiguousMatchException 。 在以下示例中,将引发异常,因为:

  • TestClass 类型具有两个 DisplayValue 方法的公共实例重载, DisplayValue(String) 以及 DisplayValue(String, Object[])

  • TestClass 类型具有方法的 Equals 两个公共实例重载,其中一个是从 ObjectEquals(TestClass)Equals(Object)继承的。

using System;
using System.Reflection;

public class TestClass
{
    public void DisplayValue(String s)
    {
        Console.WriteLine(s);
    }

    public void DisplayValue(String s, params Object[] values)
    {
        Console.WriteLine(s, values);
    }

    public static bool Equals(TestClass t1, TestClass t2)
    {
        return Object.ReferenceEquals(t1, t2);
    }

    public bool Equals(TestClass t)
    {
        return Object.ReferenceEquals(this, t);
    }
}

public class Example1
{
    public static void Main()
    {
        Type t = typeof(TestClass);

        RetrieveMethod(t, "DisplayValue", BindingFlags.Public | BindingFlags.Instance);

        RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance);

        RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Static);
    }

    private static void RetrieveMethod(Type t, String name, BindingFlags flags)
    {
        try
        {
            MethodInfo m = t.GetMethod(name, flags);
            if (m != null)
            {
                Console.Write("{0}.{1}(", t.Name, m.Name);
                ParameterInfo[] parms = m.GetParameters();
                for (int ctr = 0; ctr < parms.Length; ctr++)
                {
                    Console.Write(parms[ctr].ParameterType.Name);
                    if (ctr < parms.Length - 1)
                        Console.Write(", ");
                }
                Console.WriteLine(")");
            }
            else
            {
                Console.WriteLine("Method not found");
            }
        }
        catch (AmbiguousMatchException)
        {
            Console.WriteLine("The following duplicate matches were found:");
            MethodInfo[] methods = t.GetMethods(flags);
            foreach (var method in methods)
            {
                if (method.Name != name) continue;

                Console.Write("   {0}.{1}(", t.Name, method.Name);
                ParameterInfo[] parms = method.GetParameters();
                for (int ctr = 0; ctr < parms.Length; ctr++)
                {
                    Console.Write(parms[ctr].ParameterType.Name);
                    if (ctr < parms.Length - 1)
                        Console.Write(", ");
                }
                Console.WriteLine(")");
            }
        }
        Console.WriteLine();
    }
}
// The example displays the following output:
//       The following duplicate matches were found:
//          TestClass.DisplayValue(String)
//          TestClass.DisplayValue(String, Object[])
//       
//       The following duplicate matches were found:
//          TestClass.Equals(TestClass)
//          TestClass.Equals(Object)
//       
//       TestClass.Equals(TestClass)
//       
//       TestClass.Equals(TestClass, TestClass)
open System
open System.Reflection

type TestClass() =
    member _.DisplayValue(s) = 
        printfn $"%s{s}"

    member _.DisplayValue(s: string, [<ParamArray>]values: obj[]) =
        Console.WriteLine(s, values)

    member this.Equals(t: TestClass) =
        Object.ReferenceEquals(this, t)

    static member Equals(t1: TestClass, t2: TestClass) =
        Object.ReferenceEquals(t1, t2)

let retrieveMethod (t: Type) name (flags: BindingFlags) =
    try
        let m = t.GetMethod(name, flags)
        if m <> null then
            printf $"{t.Name}.{m.Name}("
            let parms = m.GetParameters()
            for i = 0 to parms.Length - 1 do
                printf $"{parms[i].ParameterType.Name}"
                if i < parms.Length - 1 then
                    printf ", "
            printfn ")"
        else
            printfn "Method not found"
    with :? AmbiguousMatchException ->
        printfn "The following duplicate matches were found:"
        let methods = t.GetMethods flags
        for method in methods do
            if method.Name = name then
                printf $"   {t.Name}.{method.Name}("
                let parms = method.GetParameters()
                for i = 0 to parms.Length - 1 do
                    printf $"{parms[i].ParameterType.Name}"
                    if i < parms.Length - 1 then
                        printf ", "
                printfn ")"
    printfn ""

let t = typeof<TestClass>

retrieveMethod t "DisplayValue" (BindingFlags.Public ||| BindingFlags.Instance)

retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Instance)

retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Instance ||| BindingFlags.DeclaredOnly)

retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Static)

// The example displays the following output:
//       The following duplicate matches were found:
//          TestClass.DisplayValue(String)
//          TestClass.DisplayValue(String, Object[])
//       
//       The following duplicate matches were found:
//          TestClass.Equals(TestClass)
//          TestClass.Equals(Object)
//       
//       TestClass.Equals(TestClass)
//       
//       TestClass.Equals(TestClass, TestClass)
Imports System.Reflection

Public Class TestClass
   Public Sub DisplayValue(s As String)
      Console.WriteLine(s)
   End Sub
   
   Public Sub DisplayValue(s As String, ParamArray values() As Object)
      Console.WriteLine(s, values)
   End Sub
   
   Public Overloads Shared Function Equals(t1 As TestClass, t2 As TestClass) As Boolean
      Return Object.ReferenceEquals(t1, t2)
   End Function
   
   Public Overloads Function Equals(t As TestClass) As Boolean
      Return Object.ReferenceEquals(Me, t)
   End Function          
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(TestClass)
      
      RetrieveMethod(t, "DisplayValue", BindingFlags.Public Or BindingFlags.Instance)

      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance)
      
      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
      
      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Static)
   End Sub
   
   Public Sub RetrieveMethod(t As Type, name As String, flags As BindingFlags)
      Try
         Dim m As MethodInfo = t.GetMethod(name, flags)
         If m IsNot Nothing Then
            Console.Write("{0}.{1}(", t.Name, m.Name)
           Dim parms() As ParameterInfo = m.GetParameters()
            For ctr As Integer = 0 To parms.Length - 1
               Console.Write(parms(ctr).ParameterType.Name)
               if ctr < parms.Length - 1 Then 
                  Console.Write(", ")
               End If      
            Next
            Console.WriteLine(")")
         Else
            Console.WriteLine("Method not found")
         End If
      Catch e As AmbiguousMatchException
         Console.WriteLine("The following duplicate matches were found:")
         Dim methods() As MethodInfo = t.GetMethods(flags)
         For Each method In methods
            If method.Name <> name Then Continue For

            Console.Write("   {0}.{1}(", t.Name, method.Name)
            Dim parms() As ParameterInfo = method.GetParameters()
            For ctr As Integer = 0 To parms.Length - 1
               Console.Write(parms(ctr).ParameterType.Name)
               if ctr < parms.Length - 1 Then 
                  Console.Write(", ")
               End If      
            Next
            Console.WriteLine(")")
         Next 
      End Try         
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       The following duplicate matches were found:
'          TestClass.DisplayValue(String)
'          TestClass.DisplayValue(String, Object[])
'       
'       The following duplicate matches were found:
'          TestClass.Equals(TestClass)
'          TestClass.Equals(Object)
'       
'       TestClass.Equals(TestClass)
'       
'       TestClass.Equals(TestClass, TestClass)

可以执行以下操作之一来检索特定方法:

  • 更改绑定约束。 在前面的示例中,尝试检索由类型声明且未成功Equals(TestClass)继承的公共实例Equals方法。

  • 调用方法的 GetMethod 重载,其中包含定义 types 方法参数类型的参数。

  • GetMethods(BindingFlags)调用该方法以检索包含属于具有指定绑定属性的类型的所有方法的数组。 然后,可以循环访问它以标识命名 name的重复方法。 此方法在上一示例的异常处理程序 AmbiguousMatchException 中进行了说明。

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

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

注释

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

另请参阅

适用于

GetMethod(String, BindingFlags, Type[])

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

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

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * System.Reflection.BindingFlags * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, types As Type()) As MethodInfo

参数

name
String

包含要获取的方法的名称的字符串。

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。 - 或 - 默认返回 null

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。 - 或 - 一个空的对象 Type 数组(由字段提供 EmptyTypes ),以获取不带参数的方法。

返回

一个对象,表示符合指定要求的方法(如果找到);否则,为 null.

属性

适用于

GetMethod(String, Int32, Type[])

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

搜索与指定的泛型参数计数和参数类型匹配的指定公共方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * Type[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type()) As MethodInfo

参数

name
String

包含要获取的公共方法的名称的字符串。

genericParameterCount
Int32

方法的泛型类型参数数。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

返回

一个对象,表示其参数与指定的泛型参数计数和参数类型匹配的公共方法(如果找到);否则,为 null.

属性

例外

namenull

-或-

typesnull

-或-

数组中的 types 元素之一是 null

genericParameterCount 为负数。

适用于

GetMethod(String, Type[])

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

搜索其参数与指定参数类型匹配的指定公共方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types);
public System.Reflection.MethodInfo GetMethod(string name, Type[] types);
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
member this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type()) As MethodInfo

参数

name
String

包含要获取的公共方法的名称的字符串。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

返回

一个对象,表示其参数与指定参数类型匹配的公共方法(如果找到);否则,为 null.

实现

属性

例外

使用指定的名称和指定的参数找到多个方法。

namenull

-或-

typesnull

-或-

其中 types 一个元素是 null

types 是多维。

示例

以下示例查找指定各种参数类型的特定重载 MethodA

注释

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


using System;
using System.Reflection;

class Program4
{
    // 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 i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int) });
    Console.WriteLine("Found method: {0}", mInfo);

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

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

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

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // member MethodA: i: int * j: int -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int> |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: int[] -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int[]> |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: nativeptr<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakePointerType() |])
    printfn $"Found method: {mInfo}"

    // member MethodA: r: byref<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakeByRefType() |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: int * o: outref<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int>.MakeByRefType() |])
    printfn $"Found method: {mInfo}"

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:

    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an out parameter. Note that an Imports
    ' reference is needed to System.Runtime.InteropServices 
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(i As Integer i, l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), GetType(Long)})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get  MethodA(i As Integer())
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer())})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer).MakeByRefType})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(i As Integer, ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), _
            GetType(Integer).MakeByRefType})
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

以下示例检索 MethodInfo 表示 Add 非泛型类型(类)、开放泛型类型( ArrayList 类)和封闭泛型类型( List<T> 类型) List(Of String) 的方法的对象。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Get a Type object that represents a non-generic type.
      GetAddMethod(typeof(ArrayList));

      var list = new List<String>();
      // Get a Type object that represents a constructed generic type.
      Type closed = list.GetType();
      GetAddMethod(closed);
      
      // Get a Type object that represents an open generic type.
      Type open = typeof(List<>);
      GetAddMethod(open);
   }

   private static void GetAddMethod(Type typ)
   {
      MethodInfo method;
      // Determine if this is a generic type.
      if (typ.IsGenericType) {
         // Is it an open generic type?
         if (typ.ContainsGenericParameters)
            method = typ.GetMethod("Add", typ.GetGenericArguments());
         // Get closed generic type arguments.
         else
            method = typ.GetMethod("Add", typ.GenericTypeArguments);
      }
      // This is not a generic type.
      else {
         method = typ.GetMethod("Add", new Type[] { typeof(Object) } );
      }

      // Test if an Add method was found.
      if (method == null) { 
         Console.WriteLine("No Add method found.");
         return;
      }   
      
      Type t = method.ReflectedType;
      Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name);
      ParameterInfo[] parms = method.GetParameters();
      for (int ctr = 0; ctr < parms.Length; ctr++)
         Console.Write("{0}{1}", parms[ctr].ParameterType.Name, 
                       ctr < parms.Length - 1 ? ", " : "");

      Console.WriteLine(")");
   }   
}
// The example displays the following output:
//       System.Collections.ArrayList.Add(Object)
//       System.Collections.Generic.List`1.Add(String)
//       System.Collections.Generic.List`1.Add(T)
open System
open System.Collections

let getAddMethod (typ: Type) = 
    let method = 
        // Determine if this is a generic type.
        if typ.IsGenericType then
            // Is it an open generic type?
            if typ.ContainsGenericParameters then
                typ.GetMethod("Add", typ.GetGenericArguments())
            // Get closed generic type arguments.
            else
                typ.GetMethod("Add", typ.GenericTypeArguments)
        // This is not a generic type.
        else
            typ.GetMethod("Add", [| typeof<obj> |])

    // Test if an Add method was found.
    if method = null then
        printfn "No Add method found."
    else
        let t = method.ReflectedType
        printf $"{t.Namespace}.{t.Name}.{method.Name}("
        let parms = method.GetParameters()
        for i = 0 to parms.Length - 1 do
            printf $"""{parms[i].ParameterType.Name}{if i < parms.Length - 1 then ", " else ""}"""
        printfn ")"

// Get a Type object that represents a non-generic type.
getAddMethod typeof<ArrayList>

let list = ResizeArray<String>()
// Get a Type object that represents a constructed generic type.
let closed = list.GetType()
getAddMethod closed

// Get a Type object that represents an open generic type.
let opn = typeof<ResizeArray<_>>.GetGenericTypeDefinition()
getAddMethod opn

// The example displays the following output:
//       System.Collections.ArrayList.Add(Object)
//       System.Collections.Generic.List`1.Add(String)
//       System.Collections.Generic.List`1.Add(T)
Imports System.Collections
Imports System.Collections.Generic
Imports System.Reflection

Module Example
   Public Sub Main()
      ' Get a Type object that represents a non-generic type.
      GetAddMethod(GetType(ArrayList))
      
      Dim list As New List(Of String)()
      ' Get a Type object that represents a constructed generic type.
      Dim closed As Type = list.GetType()
      GetAddMethod(closed)
      
      ' Get a Type object that represents an open generic type.
      Dim open As Type = GetType(List(Of))
      GetAddMethod(open)
   End Sub
   
   Private Sub GetAddMethod(typ As Type)
      Dim method As MethodInfo
      ' Determine if this is a generic type.
      If typ.IsGenericType Then
         ' Is it an open generic type?
         If typ.ContainsGenericParameters Then
            method = typ.GetMethod("Add", typ.GetGenericArguments())
         ' Get closed generic type arguments.
         Else
            method = typ.GetMethod("Add", typ.GenericTypeArguments)
         End If
      ' This is not a generic type.
      Else
         method = typ.GetMethod("Add", { GetType(Object) } )
      End If
      ' Test if an Add method was found.
      If method Is Nothing Then 
         Console.WriteLine("No Add method found.")
         Exit Sub
      End If   

      Dim t As Type = method.ReflectedType
      Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name)
      Dim params() As ParameterInfo = method.GetParameters()
      For ctr As Integer = 0 To params.Length - 1
         Console.Write("{0}{1}", params(ctr).ParameterType.Name, 
                       If(ctr < params.Length - 1, ", ", ""))
      Next
      Console.WriteLine(")")
   End Sub
End Module
' The example displays the following output:
'       System.Collections.ArrayList.Add(Object)
'       System.Collections.Generic.List`1.Add(String)
'       System.Collections.Generic.List`1.Add(T)

该示例定义检索 GetAddMethod 相应 MethodInfo 对象的方法。 若要为打开的泛型类型提供 types 参数,它将调用该方法 Type.GetGenericArguments 。 若要提供 types 封闭泛型类型的参数,它将检索属性的值 Type.GenericTypeArguments

注解

搜索 name 区分大小写。 搜索包括公共静态和公共实例方法。

注释

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

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

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

注释

name 参数不能包含类型参数。 例如,C# 代码GetMethod("MyGenericMethod<int>")搜索文本名称为“”MyGenericMethod<int>的方法,而不是类型为一个泛型参数int的方法MyGenericMethodGetMethod("MyGenericMethod")请改用数组中的types相应参数。

另请参阅

适用于

GetMethod(String)

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

搜索具有指定名称的公共方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name);
public System.Reflection.MethodInfo GetMethod(string name);
public System.Reflection.MethodInfo? GetMethod(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string -> System.Reflection.MethodInfo
member this.GetMethod : string -> System.Reflection.MethodInfo
abstract member GetMethod : string -> System.Reflection.MethodInfo
override this.GetMethod : string -> System.Reflection.MethodInfo
Public Function GetMethod (name As String) As MethodInfo

参数

name
String

包含要获取的公共方法的名称的字符串。

返回

一个对象,表示具有指定名称的公共方法(如果找到);否则,为 null.

实现

属性

例外

使用指定名称找到多个方法。

namenull

示例

以下示例获取名为 . MethodA.


using System;
using System.Reflection;

class Program
{

    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {

        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA");
        Console.WriteLine("Found method: {0}", mInfo);
    }
}
type Program() =
    // Method to get:
    member _.MethodA() = ()

// Get MethodA()
let mInfo = typeof<Program>.GetMethod "MethodA"
printfn $"Found method: {mInfo}"

Imports System.Reflection

Class Program

    ' Method to get:
    Public Sub MethodA()
    End Sub


    Public Shared Sub Main(ByVal args() As String)

        ' Get MethodA()
        Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA")
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

注解

搜索 name 区分大小写。 搜索包括公共静态和公共实例方法。

如果方法重载并具有多个公共方法,该方法 GetMethod(String) 将引发异常 AmbiguousMatchException 。 在以下示例中,由于方法有多个公共重载 Int32.ToString ,因此会引发异常。 另一方面,由于 Person.ToString 方法重写 Object.ToString ,因此不会重载, GetMethod(String) 因此该方法能够检索 MethodInfo 对象。

using System;
using System.Reflection;

public class Person
{
    public String FirstName;
    public String LastName;

    public override String ToString()
    {
        return (FirstName + " " + LastName).Trim();
    }
}

public class Example2
{
    public static void Main()
    {
        Type t = typeof(Person);
        RetrieveMethod(t, "ToString");

        t = typeof(Int32);
        RetrieveMethod(t, "ToString");
    }

    private static void RetrieveMethod(Type t, String name)
    {
        try
        {
            MethodInfo m = t.GetMethod(name);
            if (m != null)
                Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
                                  m.Name, m.IsStatic ? "Static" : "Instance");
            else
                Console.WriteLine("{0}.ToString method not found", t.Name);
        }
        catch (AmbiguousMatchException)
        {
            Console.WriteLine("{0}.{1} has multiple public overloads.",
                              t.Name, name);
        }
    }
}
// The example displays the following output:
//       Person.ToString: Instance method
//       Int32.ToString has multiple public overloads.
open System
open System.Reflection

type Person() =
    member val FirstName = "" with get, set
    member val LastName = "" with get, set

    override this.ToString() =
        (this.FirstName + " " + this.LastName).Trim()

let retrieveMethod (t: Type) name =
    try
        let m = t.GetMethod name
        if m <> null then
            printfn $"""{m.ReflectedType.Name}.{m.Name}: {if m.IsStatic then "Static" else "Instance"} method"""
        else
            printfn $"{t.Name}.ToString method not found"
    with :? AmbiguousMatchException ->
        printfn $"{t.Name}.{name} has multiple public overloads."

let t = typeof<Person>
retrieveMethod t "ToString"

let t2 = typeof<int>
retrieveMethod t2 "ToString"

// The example displays the following output:
//       Person.ToString: Instance method
//       Int32.ToString has multiple public overloads.
Imports System.Reflection

Public Class Person
   Public FirstName As String
   Public LastName As String
   
   Public Overrides Function ToString() As String
      Return (FirstName + " " + LastName).Trim()
   End Function
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(Person)
      RetrieveMethod(t, "ToString")
      
      t = GetType(Int32)
      RetrieveMethod(t, "ToString")
   End Sub
   
   Private Sub RetrieveMethod(t As Type, name As String)   
      Try
         Dim m As MethodInfo = t.GetMethod(name)
         If m IsNot Nothing Then
            Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
                              m.Name, If(m.IsStatic, "Static", "Instance"))    
         Else
            Console.WriteLine("{0}.ToString method not found", t.Name)
         End If   
      Catch e As AmbiguousMatchException
         Console.WriteLine("{0}.{1} has multiple public overloads.", 
                           t.Name, name)
      End Try
   End Sub
End Module
' The example displays the following output:
'       Person.ToString: Instance method
'       Int32.ToString has multiple public overloads.

可以执行以下操作之一来检索特定方法:

  • GetMethod(String, BindingFlags)调用该方法并指定唯一bindingAttr标识方法的参数。 例如,如果由于类型具有静态和实例重载而引发异常,则可以指定bindingAttrBindingFlags.Instance参数BindingFlags.Static

  • 调用方法的 GetMethod 重载,其中包含定义 types 方法参数类型的参数。

  • GetMethods()调用该方法以检索包含属于类型的所有公共方法的数组。 然后,可以循环访问它以标识命名 name的重复方法。

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

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

注释

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

另请参阅

适用于

GetMethod(String, Type[], ParameterModifier[])

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

搜索其参数与指定参数类型和修饰符匹配的指定公共方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type(), modifiers As ParameterModifier()) As MethodInfo

参数

name
String

包含要获取的公共方法的名称的字符串。

types
Type[]

一个 Type 对象数组,表示要获取的方法的参数的数量、顺序和类型。

-或-

一个由对象(由字段提供EmptyTypes)的Type空数组,用于获取不采用任何参数的方法。

modifiers
ParameterModifier[]

表示与数组中types相应元素关联的属性的对象数组ParameterModifier。 仅在通过 COM 互操作调用时使用,并且仅处理通过引用传递的参数。 默认绑定器不处理此参数。

返回

一个对象,表示符合指定要求的公共方法(如果找到);否则,为 null.

实现

属性

例外

使用指定的名称和指定的参数找到多个方法。

namenull

-或-

typesnull

-或-

其中 types 一个元素是 null

types 是多维。

-或-

modifiers 是多维。

注解

尽管默认联编程序不处理 ParameterModifiermodifiers 参数),但可以使用抽象 System.Reflection.Binder 类编写一个处理自定义 modifiers联编程序。 ParameterModifier 仅在通过 COM 互作调用时使用,并且仅处理通过引用传递的参数。

搜索 name 区分大小写。 搜索包括公共静态和公共实例方法。

注释

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

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

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

注释

对于泛型方法,请勿在 . 中 name包括类型参数。 例如,C# 代码GetMethod("MyMethod<int>")搜索文本名称为“”MyMethod<int>的成员,而不是具有一个类型int泛型参数的方法MyMethodGetMethod("MyMethod")请改用数组中的types相应参数。

另请参阅

适用于