开发者

Specifying generic delegate type param at runtime

following setup, i have several generic functions, and i need to choose the type and the function identified by two strings at runtime.

my first try looked like this:

public static class FOOBAR
{
    public delegate void MyDelegateType(int param);

    public static void foo<T>(int param){...}
    public static void bar<T>(int param){...}

    public static void someMethod(string methodstr, string typestr)
    {
        MyDelegateType mydel;
        Type 开发者_运维问答mytype;
        switch(typestr)
        {
            case "int": mytype = typeof(int); 
                        break;
            case "double": mytype = typeof(double); 
                           break;
            default: throw new InvalidTypeException(typestr);
        }
        switch(methodstr)
        {
            case "foo": mydel = foo<mytype>; //error
                        break;
            case "bar": mydel = bar<mytype>; //error
                        break;
            default: throw new InvalidTypeException(methodstr);
        }
        for(int i=0; i<1000; ++i)
            mydel(i);
    }
}

since this didnt work, i nested those switchs (a methodstr switch inside the typestr switch or viceversa), but that solution is really ugly and unmaintainable.

The number of types is pretty much fixed, but the number of functions like foo or bar will increase by high numbers, so i dont want nested switchs.

So how can i make this working without using nested switchs ?


You need to use Reflection:

MethodInfo method = typeof(FooBar).GetMethod(methodStr, BindingFlags.Static);
Type genericParam = Type.Parse(typestr);

MethodInfo genericMethod = method.MakeGenericMethod(genericParam);

for(int i=0; i<1000; ++i)
    genericMethod.Invoke(null, new object[] { i });

If the (non-generic) signature of the method will always be the same, it will be faster to create a delegate, like this:

Action<int> del = Delegate.CreateDelegate(typeof(Action<int>), null, genericMethod);

for(int i=0; i<1000; ++i)
    del(i);


Look at the documentation for Delegate.CreateDelegate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜