As mentioned in the title I have an Expression where the result type is 'stored' in an object
, which can be all of my domain classes. Is there a way to get the concrete expression (unboxing)?
Asked
Active
Viewed 825 times
2

Sergey Kalinichenko
- 714,442
- 84
- 1,110
- 1,523

core
- 851
- 1
- 8
- 28
-
What kind of expressions could be used in lambda's body? – Dennis Dec 23 '13 at 13:38
1 Answers
3
If I understood your question correctly, what you might be looking for is Expression.Convert
Expression<Func<T,object>> original = // ... something
Expression<Func<T,TResult>> converted = Expression.Lambda<Func<T,TResult>>(
Expression.Convert(original.Body,typeof(TResult)),
original.Parameters);

Sergey Kalinichenko
- 714,442
- 84
- 1,110
- 1,523

Olivier
- 5,578
- 2
- 31
- 46
-
Thank you, thats working! But still one question: When I need this unboxed expression I don't know the type of TResult (beside that is implementing IPersistable). I could remember/getting the type and packing the call `Unbox
(f1)` in a switch case expression. But do you know a more comfortable way? – core Dec 23 '13 at 16:01 -
How would you want to get a Func
if you dont know what TResult is ? Do you at least have a System.Type object representing TResult ? – Olivier Dec 23 '13 at 20:27 -
To getting or holding the type would be no problem. But could this question be worth for a new thread? – core Dec 25 '13 at 09:55
-
What I meant is: What is the use of getting a Expression
> if are not in a statically typed context for TResult type ? (you wont be able to use it as a proper Func – Olivier Dec 26 '13 at 10:47!) If you're only interested by the Expression, just use Expression.Lambda() without generic argument (and assign it to "Expression" type only)