我如何分辨.NET中使用GetMethod的通用和非通用签名的?NET、GetMethod

2023-09-04 07:11:25 作者:王者路开大奔你大哥

假设存在一个X类,如下所述,如何获取信息的方法对非泛型方法?下面的code会抛出异常。

 使用系统;

类节目{
    静态无效的主要(字串[] args){
        变种MI = Type.GetType(X)GetMethod的(Y)。 //暧昧找到匹配。
        Console.WriteLine(mi.ToString());
    }
}

类X {
    公共无效Y(){
        Console.WriteLine(我想这一个);
    }
    公共无效Y'LT; T>(){
        Console.WriteLine(没有这一项);
    }
}
 

解决方案

不要使用 GetMethod的,使用的getMethods ,然后检查 IsGenericMethod

 使用系统;
使用System.Linq的;

类节目
{
    静态无效的主要(字串[] args)
    {
        变种MI = Type.GetType(X)的getMethods()如(方法=> method.Name ==Y)。;
        Console.WriteLine(mi.First()名+通用?+ mi.First()IsGenericMethod。);
        Console.WriteLine(mi.Last()名+通用?+ mi.Last()IsGenericMethod。);
    }
}

类X
{
    公共无效Y()
    {
        Console.WriteLine(我想这一个);
    }
    公共无效Y'LT; T>()
    {
        Console.WriteLine(没有这一项);
    }
}
 

作为奖励 - 扩展方法:

 公共静态类TypeExtensions
{
    公共静态MethodInfo的GetMethod的(这种类型的字符串名称,布尔通用)
    {
        如果(类型== NULL)
        {
            抛出新ArgumentNullException(类);
        }
        如果(String.IsNullOrEmpty(名))
        {
            抛出新ArgumentNullException(姓名);
        }
        返回type.GetMethods()
            .FirstOrDefault(方法=> method.Name ==名和放大器; method.IsGenericMethod ==通用);
    }
}
 
delphi7无法编译已经使用的单元怎么解决 使用VS Code 开发.NET Core程序指南

然后只需:

 静态无效的主要(字串[] args)
{
    。MethodInfo的通用= Type.GetType(X)GetMethod的(Y,真);
    。MethodInfo的非泛型= Type.GetType(X)GetMethod的(Y,假);
}
 

Assuming there exist a class X as described below, how do I get method information for the non-generic method? The code below will throw an exception.

using System;

class Program {
    static void Main(string[] args) {
        var mi = Type.GetType("X").GetMethod("Y"); // Ambiguous match found.
        Console.WriteLine(mi.ToString());
    }
}

class X {
    public void Y() {
        Console.WriteLine("I want this one");
    }
    public void Y<T>() {
        Console.WriteLine("Not this one");
    }
}

解决方案

Don't use GetMethod, use GetMethods, then check IsGenericMethod.

using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var mi = Type.GetType("X").GetMethods().Where(method => method.Name == "Y"); 
        Console.WriteLine(mi.First().Name + " generic? " + mi.First().IsGenericMethod);
        Console.WriteLine(mi.Last().Name + " generic? " + mi.Last().IsGenericMethod);
    }
}

class X
{
    public void Y()
    {
        Console.WriteLine("I want this one");
    }
    public void Y<T>()
    {
        Console.WriteLine("Not this one");
    }
}

As a bonus - an extension method:

public static class TypeExtensions
{
    public static MethodInfo GetMethod(this Type type, string name, bool generic)
    {
        if (type == null)
        {
            throw new ArgumentNullException("type");
        }
        if (String.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException("name");
        }
        return type.GetMethods()
            .FirstOrDefault(method => method.Name == name & method.IsGenericMethod == generic);
    }
}

Then just:

static void Main(string[] args)
{
    MethodInfo generic = Type.GetType("X").GetMethod("Y", true);
    MethodInfo nonGeneric = Type.GetType("X").GetMethod("Y", false);
}