I have some ugly code that looks something like this:
switch (f.TypeName)
{
case "TypeOne":
ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod<TypeOne>), args);
break;
case "TypeTwo":
ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod<TypeTwo>), args);
break;
...
case "TypeTwenty":
ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod<TypeTwenty>), args);
break;
}
I would really like to get rid of the switch statement and do something along these lines:
var myTypeDescriptor = Type.GetType(f.TypeName); //<== clearly not this, but similar
ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod<myTypeDescriptor>), args);
where MyMethod looks like this:
protected void MyMethod<T>(object args)
{
...
}
I've poked around some and have found a number of examples where the generic is a generic class, but no examples where it is a generic method. And definitely not any examples where it is being passed to a delegate.
While this appears, at first glance, to be a duplicate of How do I use reflection to call a generic method?, I am not simply trying to call the method. It is being passed to the WaitCallback delegate. Perhaps this is a two part question: 1) how do I retrieve the generic method via reflection and 2) having obtained a handle to the method, how do I pass it as an argument to the WaitCallback delegate?
Anyone have any ideas? Is this even possible?