您好我使用Linq Expression BuildTree逻辑,最后编译并运行。
Expression.Lambda<Func<T, bool>>(expr, paramUser).Compile();
class MyClass
{
List<Param> params{get;set;}
}
class Params{
string Key,
string Value
}
我想在运行时创建动态表达式,传递键和值参数并构建我的表达式树,我该如何为这些case.I构建我的表达式。
if (ExpressionType.TryParse(r.Operator, out tBinary))
{
var right = this.StringToExpression(r.TargetValue, propType);
return Expression.MakeBinary(tBinary, propExpression, right);
}
else if (r.Operator == "IsMatch")
{
return Expression.Call(
typeof(Regex).GetMethod("IsMatch",
new[] { typeof(string), typeof(string), typeof(RegexOptions) }),
propExpression,
Expression.Constant(r.TargetValue, typeof(string)),
Expression.Constant(RegexOptions.IgnoreCase, typeof(RegexOptions))
);
}