我如何分辨.NET中使用GetMethod的通用和非通用签名的? [英] How do I distinguish between generic and non generic signatures using GetMethod in .NET?

查看:91
本文介绍了我如何分辨.NET中使用GetMethod的通用和非通用签名的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设存在一个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 ==通用);
    }
}
 

然后只需:

 静态无效的主要(字串[] 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);
}

这篇关于我如何分辨.NET中使用GetMethod的通用和非通用签名的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆