jeudi 12 mai 2016

Find the generic method on a static class

I have a class

public static class MyClass
{

    public static T MyMethod<T>(T item) where T : ISomeInterface<T>, new
    {
        return MyMethod(new[] { item}).First();
    }

    public static IEnumerable<T> MyMethod<T>(params T[] items) where T : ISomeInterface<T>, new
    {
         // for simplicity
         return items.ToList();
    }
}

and a bunch of even more complex overloads. Now I want to extend the class (because I want to call if from powershell) with

    public static IEnumerable MyMethod(string typeName, params object[] items)
    {
        var type = Type.GetType(typeName, true, true);
        var paramTypes = new Type[] { type.MakeArrayType() };
        var method = typeof(MyClass).GetMethod(
            "MyMethod", BindingFlags.Public | BindingFlags.Static
                | BindingFlags.IgnoreCase, null, paramTypes, null);
        return method.Invoke(null, new object[] { items });
    }

But method is always null. Which is the correct way to get my specific method via GetMethod().





Aucun commentaire:

Enregistrer un commentaire