Expression <Func <T, bool> > CreateLambdaBinary(CRLExpression expression) { var left = expression.Left; var right = expression.Right; var creater = new LambdaCreater <T>(); var type = expression.ExpressionType; var dic = new Dictionary <string, ExpressionHandler>(); dic.Add("Equal", creater.Equal); dic.Add("NotEqual", creater.NotEqual); dic.Add("Greater", creater.Greater); dic.Add("Less", creater.Less); dic.Add("GreaterThan", creater.GreaterThan); dic.Add("LessThan", creater.LessThan); //todo 更多方法解析 if (!dic.ContainsKey(type)) { throw new CRLException("没有对应的运算方法 " + type); } if (left.Type == CRLExpressionType.MethodCall)//按方法运算 如 b.NameSubstring,Indexof(0,1)=="123" { //按属性的子方法 var left2 = CreateLambdaMethodCall2(left); var value = ObjectConvert.ConvertObject(left2.Type, right.Data); return(dic[type](left2, value)); } else//按属性运算 如b.Id==1 { var member = creater.CreatePropertyExpression(left.Data.ToString()); return(dic[type](member, right.Data)); } }
internal MethodCallExpression CreateLambdaMethodCall2(CRLExpression expression) { //表示二元运算的方法 var creater = new LambdaCreater <T>(); var arry = expression.Data.ToString().Split('|'); var propertyName = arry[0]; var methodName = arry[1]; var args = expression.Data.ToString().Substring(propertyName.Length + methodName.Length + 2); var dic = new Dictionary <string, MethodResultHandler>(); dic.Add("Substring", creater.Substring); dic.Add("IndexOf", creater.IndexOf); if (!dic.ContainsKey(methodName)) { throw new CRLException("没有对应的方法 " + methodName); } return(dic[methodName](propertyName, args.Split(','))); }
Expression <Func <T, bool> > CreateLambdaMethodCall(CRLExpression expression) { //表示方法调用 var creater = new LambdaCreater <T>(); var arry = expression.Data.ToString().Split('|'); var propertyName = arry[0]; var methodName = arry[1]; var args = expression.Data.ToString().Substring(propertyName.Length + methodName.Length + 2); var dic = new Dictionary <string, MethodHandler>(); dic.Add("Contains", creater.Contains); dic.Add("StartsWith", creater.StartsWith); dic.Add("EndsWith", creater.EndsWith); //todo 更多方法解析 if (!dic.ContainsKey(methodName)) { throw new CRLException("没有对应的方法 " + methodName); } return(dic[methodName](propertyName, args.Split(','))); }
public CRLExpressionVisitor() { //由于变量作用域的问题,只实例化一次,并且不能用缓存 creater = new LambdaCreater <T>(); }