简体   繁体   English

通过反射获取C#中的扩展方法的泛型重载

[英]Getting generic overload by reflection for extension method in C#

I have following extensions class: 我有以下扩展程序类:

public static class KeyValueConfigurationCollectionExtensions
{
    public static string Get(this KeyValueConfigurationCollection collection, string key)
    {
        return collection[key].Value;
    }
    public static T Get<T>(this KeyValueConfigurationCollection collection, string key)
    {
        return (T)Convert.ChangeType(collection[key].Value, typeof(T));
    }
}

Could someone tell me how can I get overload for generic method (the 2nd one from the above example) by reflection? 有人可以告诉我如何通过反射使泛型方法(上例中的第二个方法)重载吗? There is AmbiguousMatchException thrown at runtime for this line of code: 此行代码在运行时引发AmbiguousMatchException

MethodInfo method = typeof(KeyValueConfigurationCollectionExtensions).GetMethod("Get");

I know that there is an overload for GetMethod function where I can specify parameters but in that case those are the same for both methods. 我知道GetMethod函数有一个重载,我可以在其中指定参数,但在那种情况下,这两个方法的重载是相同的。 It would be great to solve that as I need it for my acceptance tests. 很好解决这个问题,因为我需要进行验收测试。


Update 更新

The simplest solution for me was: 对我来说最简单的解决方案是:

MethodInfo methodInfo = typeof(KeyValueConfigurationCollectionExtensions)
    .GetMethods().Single(method => method.Name == "Get" && method.IsGenericMethod);

Thanks guys for quick answers and wish you nice day :) 谢谢你们的快速解答,并祝您有美好的一天:)

There´s also IsGenericMethod defined on MethodInfo : 还有在MethodInfo定义的IsGenericMethod

MethodInfo method = typeof(KeyValueConfigurationCollectionExtensions).GetMethods()
    .FirstOrDefault(x => g.Name == "Get" && x.IsGenericMethod);

You need to get the method that has generic arguments by calling GetGenericArguments() , as in this example Linqpad: 您需要通过调用GetGenericArguments()来获得具有通用参数的方法,如以下示例Linqpad所示:

void Main()
{
    var method = typeof(KeyValueConfigurationCollectionExtensions)
        .GetMethods()
        .Where(m => m.Name == "Get")
        .Where(m => m.GetGenericArguments().Any())
        .Single()
        .Dump();

}

// Define other methods and classes here
public static class KeyValueConfigurationCollectionExtensions
{
    public static string Get(this KeyValueConfigurationCollection collection, string key)
    {
        return collection[key].Value;
    }
    public static T Get<T>(this KeyValueConfigurationCollection collection, string key)
    {
        return (T)Convert.ChangeType(collection[key].Value, typeof(T));
    }
}

public class KeyValueConfigurationCollection
{
    public KeyValuePair<string, string> this [string key]
    {
        get
        {
            return new KeyValuePair<string, string>("KEY: " + key, "VALUE: Hi!");
        }
    }
}

如果您有重载的方法,则必须对linq使用GetMethods方法:

MethodInfo method = typeof(KeyValueConfigurationCollectionExtensions).GetMethods().FirstOrDefault(m=>m.Name=="Get"&&m.ContainsGenericParameters==true).MakeGenericMethod(new Type[]{yourtype});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM