/// <summary> /// 匹配一种规则 /// </summary> public static Func <T, bool> CompileRule <T>(RuleModule r) { var paramUser = Expression.Parameter(typeof(T)); Expression expr = BuildExpr <T>(r, paramUser); return(Expression.Lambda <Func <T, bool> >(expr, paramUser).Compile()); }
/// <summary> /// 匹配两种规则 /// </summary> public static Func <T, bool> CompileRule <T>(RuleModule leftrule, RuleModule rightrule) { var paramUser = Expression.Parameter(typeof(T)); Expression leftexpr = BuildExpr <T>(leftrule, paramUser); Expression rightexpr = BuildExpr <T>(rightrule, paramUser); Expression express = Expression.AndAlso(leftexpr, rightexpr); return(Expression.Lambda <Func <T, bool> >(express, paramUser).Compile()); }
/// <summary> /// 构建lambra表达式树 /// </summary> private static Expression BuildExpr <T>(RuleModule r, ParameterExpression param) { var left = MemberExpression.Property(param, r.MemberName); var tProp = typeof(T).GetProperty(r.MemberName).PropertyType; ExpressionType tBinary; if (ExpressionType.TryParse(r.Operator, out tBinary)) { var right = Expression.Constant(Convert.ChangeType(r.TargetValue, tProp)); return(Expression.MakeBinary(tBinary, left, right)); } else { var method = tProp.GetMethod(r.Operator); var tParam = method.GetParameters()[0].ParameterType; var right = Expression.Constant(Convert.ChangeType(r.TargetValue, tParam)); return(Expression.Call(left, method, right)); } }