smerlin
smerlin

Reputation: 6566

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 ?

Upvotes: 1

Views: 1805

Answers (2)

SLaks
SLaks

Reputation: 887453

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);

Upvotes: 4

leppie
leppie

Reputation: 117240

Look at the documentation for Delegate.CreateDelegate

Upvotes: 0

Related Questions