I have the following method where T
is used inside a Func
:
public void DoSomething<T>(string someString, Func<T, bool> someMethod)
{
if(someCondition)
{
string A;
bool resultA = someMethod(A);
}
else
{
string[] B;
bool resultB = someMethod(B);
}
// Some other stuff here ...
}
I am invoking the DoSomething
method in the following manner:
DoSomething<string>("abc", someMethod);
DoSomething<string[]>("abc", someMethod);
And the someMethod exists with the following definitions:
bool someMethod(string simpleString);
bool someMethod(string[] stringArray);
Now the compilation fails with the following errors in method DoSomething
:
cannot convert from 'string' to 'T'
cannot convert from 'string[]' to 'T'
I am unable to figure out if there is a solution to the problem, or what I am trying is not feasible. It looks similar to question How can I pass in a func with a generic type parameter?, though it was not helpful for my scenario.