In my code, I am trying to dynamically resolve which type parameter should be used for a generic method call.
private int FooResolver<T1, T2>(bool condition, Func<int> fooMethod)
{
if (condition)
{
return fooMethod<T1>();
}
return fooMethod<T2>();
}
I have created a wrapper method that takes in both type parameters and decides which one to use based on a condition... However, it seems C# does not allow this behavior. Are there any particular reasons for that? And is there a viable workaround for my code?
EDIT
After seeing the responses, I have decided to defer the type definition to the caller of the handler method:
private int FooResolver(bool condition, Func<int> foo1, Func<int> foo2)
{
if (condition)
{
return foo1();
}
return foo2();
}
...
private int Bar()
{
return FooResolver(myCondition, MyMethod<FirstType>, MyMethod<SecondType>);
}