我有一个方法接收一个Expression<Func<MyObject, object>>
我想通过调用object
. 扩展表达式的结果将始终为bool
. 本质上我想“转换”Expression<Func<MyObject, object>>
为Expression<Func<MyObject, bool>>
.
这是我想做的事情的要点。我意识到这不会按原样编译ReportExpr
type Expression<Func<MyObject, bool>>
, not MethodCallExpression
,但我认为这传达了意图:
private MyObjectData CreateMyObjectData(string description,
FieldTypes fieldType, Expression<Func<MyObject, object>> expression)
{
var data= new MyObjectData()
{
ReportDesc = description,
FieldType = fieldType,
};
var method = typeof(DateTime?).GetMethod("Between");
Expression<Func<MyObject, DateTime?>> from = x => x.FromValue as DateTime?;
Expression<Func<MyObject, DateTime?>> to = x => x.ToValue as DateTime?;
var methodCallExpression = Expression.Call(expression, method, from, to);
data.ReportExpr = methodCallExpression;
return data;
}