我需要得到Lambda的左/右手边
ie for(x =〉x.Foo〈x.Bar)我想同时计算x.Foo和x. Bar。lambda表达式都保证是二进制的
using System.Linq.Expressions;
var ExampleCheck = new FooChecker<M>(x => x.Foo < x.Bar);
var evalLeftHandSide = EvalLeftHandSide<M>(ExampleCheck.CheckToMake);
double EvalLeftHandSide<T>(Func<T, bool> CheckToMake)
{
Expression<Func<T, bool>> CheckAsExpression = (x) => CheckToMake(x);
//use the incoming expression
BinaryExpression be = CheckAsExpression.Body as BinaryExpression; <<<always null
Expression LHS;
if (be == null)
return 0;
LHS = be.Left;
// provide parameters of incoming expression to lambda
var LHSCompiled = Expression.Lambda(LHS, CheckAsExpression.Parameters).Compile();
// provide the incoming parameter to the invocation
var LHSValue = LHSCompiled.DynamicInvoke(Activator.CreateInstance<T>());
return Convert.ToDouble(LHSValue);
}
struct FooChecker<T>
{
public FooChecker(Func<T, bool> _CheckToMake) { CheckToMake = _CheckToMake; }
public Func<T, bool> CheckToMake;
}
class M
{
public double Foo { get; set; }
public double Bar { get; set; }
}
我无法将表达式转换为BinaryExpression,因为它实际上是一个InvocationExpression...是否可以从InvocationExpression〉BinaryExpression中获取,或者单独计算InvocationExpression左侧/右侧的方法?
干杯
1条答案
按热度按时间yyyllmsg1#
假设您传递的是
x => x.Foo < x.Bar
表达式,您要执行的操作的第一近似值可能如下所示:注解表示对原始代码的更改。
注意,这还远远不够完美--它做了很多假设(比如类型有无参数的ctor,考虑
new()
泛型约束或以其他方式提供T
,或者左侧表达式结果可转换为double
)。