samedi 30 septembre 2017

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:

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. It would be great to solve that as I need it for my acceptance tests.





Aucun commentaire:

Enregistrer un commentaire