Reputation: 19544
I have no doubt this is as easy to do as possible, but I have a function creator library that creates lambda functions for me of the form:
Func<T1, object>
And I'm looking to specify the out parameter more specifically - Basically, I'm looking to be able to create something along the lines of:
private Func<T1, T2> GetFunc<T1, T2>(string expression)
{
Func<T1, object> objFunc = CreateFunction(expression));
return objFunc as Func<T1, T2>;
}
But, when I try this, I get back a null
(as an aside, if I return objFunc
as a Func<T1, object>
it is not a null, so I know that's not where my issue lay).
How do I do this correctly?
Upvotes: 13
Views: 1101
Reputation: 2608
Try:
private Func<T1, T2> GetFunc<T1, T2>(string expression)
{
Func<T1, object> objFunc = CreateFunction(expression));
return arg=>(T2)objFunc(arg);
}
Upvotes: 16
Reputation: 156459
Would it work to simply wrap your existing function call in another one that performs a cast on the returned value?
return t1 => (T2)objFunc(t1);
Upvotes: 10