Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting an expression to another one

I have following expression

Expression<Func<T, object>> expr1;

Is there any way to cast it to

Expression<Func<IUpdateConfiguration<T>, object>>?

[Update]

Or create a new Expression<Func<IUpdateConfiguration<T>, object>> from the existing Expression<Func<T, object>>?

like image 951
Masoud Avatar asked Oct 21 '22 05:10

Masoud


1 Answers

No. The first is a function that takes a T and returns an object. The second one accepts a IUpdateConfiguration<T> and returns an object. Unless the T is also a IUpdateConfiguration<T>, you cannot cast this. If you know of a way to convert a IUpdateConfiguration<T> into a T, you can make a new expression, but that's different than casting.

For example, given this:

Expression<Func<IUpdateConfiguration<T>, T> expr2;

You can make your desired function like this:

Expression<Func<IUpdateConfiguration<T>, object>> = 
    (IUpdateConfiguration<T> t) => expr1(expr2(t));

But this will have a completely different expression body than the original one. That may or may not be a problem, depending what you're trying to accomplish.

like image 165
recursive Avatar answered Oct 22 '22 20:10

recursive