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 ?