protected override Expression VisitBinary(BinaryExpression node) { string left, right, op, condition; Match match; Visit(node.Left); bool singleBool = false; if (node.Left.NodeType == ExpressionType.MemberAccess) { var leftExp = node.Left as MemberExpression; if (ExpressionReflector.GetNullableOrSelfType(leftExp.Type).FullName == "System.Boolean" && leftExp.Type.Assembly.GlobalAssemblyCache) { //x.IsEnabled这种情况 if (node.Right.NodeType == ExpressionType.Constant) { //x.IsEnabled=true } else if (_conditionStack.Count > 0) { //x.IsEnabled && ...... _list.Add(_conditionStack.Peek(), true);//.Push("[" + leftExp.Member.Name + "]"); singleBool = true; } } } if (node.Left.NodeType == ExpressionType.Not) { var unaryExp = node.Left as UnaryExpression; if (unaryExp.Operand.NodeType == ExpressionType.MemberAccess) { var leftExp = unaryExp.Operand as MemberExpression; if (leftExp.Type.FullName == "System.Boolean" && leftExp.Type.Assembly.GlobalAssemblyCache) { //x.IsEnabled这种情况 if (node.Right.NodeType == ExpressionType.Constant) { //x.IsEnabled=true } else if (_conditionStack.Count > 0) { //x.IsEnabled && ...... _list.Add(_conditionStack.Peek(), false);//.Push("[" + leftExp.Member.Name + "]"); singleBool = true; } else { bool value = (bool)_constStack.Pop(); if (value) { _conditionStack.Push("1=1"); } else { _conditionStack.Push("1=2"); } } } } } //此时已获取到了值,一般情况下访问右支就是为了找值,所以直接组合 if (singleBool) { right = _conditionStack.Pop(); left = _conditionStack.Pop(); op = "="; condition = string.Format("({0} {1} {2})", left, op, right); _conditionStack.Push(condition); } if (node.NodeType == ExpressionType.ArrayIndex) { //处理数组访问,此时right存的是index,所以跳过Right访问 var constExp = node.Right as ConstantExpression; var index = Convert.ToInt32(constExp.Value); var arrayObject = _constStack.Pop() as Array; object arrayValue = arrayObject.GetValue(index); while (_memberStack.Count > 0) { var member = _memberStack.Pop(); if (member.MemberType == MemberTypes.Field) { arrayValue = ((FieldInfo)member).GetValue(arrayValue); } else if (member.MemberType == MemberTypes.Property) { arrayValue = ((PropertyInfo)member).GetValue(arrayValue, null); } else { throw new NotSupportedException("未知的成员类型:" + member.MemberType); } } _list.Add(_conditionStack.Peek(), arrayValue); return(node); } else { if (node.Right is ConstantExpression) { var constExp = node.Right as ConstantExpression; _list.Add(_conditionStack.Peek(), constExp.Value); } else { Visit(node.Right); } } if (_constStack.Count > 0) { //常量计算 object c1 = _constStack.Pop(); object c2 = _constStack.Pop(); bool c3 = false; switch (node.NodeType) { case ExpressionType.Equal: c3 = c1 == c2; break; case ExpressionType.GreaterThan: if (c1 == null && c2 == null) { c3 = true; } else { c3 = false; } break; } if (c3) { _conditionStack.Push("1=1"); } else { _conditionStack.Push("1=2"); } } else { right = _conditionStack.Pop(); left = _conditionStack.Pop(); op = string.Empty; switch (node.NodeType) { case ExpressionType.AndAlso: op = "and"; break; case ExpressionType.OrElse: op = "or"; break; case ExpressionType.Equal: op = "="; break; case ExpressionType.GreaterThanOrEqual: op = ">="; break; case ExpressionType.GreaterThan: op = ">"; break; case ExpressionType.LessThan: op = "<"; break; case ExpressionType.LessThanOrEqual: op = "<="; break; case ExpressionType.ArrayIndex: op = "="; break; default: throw new NotImplementedException("未实现逻辑关系:" + node.NodeType); } var lastParameter = _list.LastOrDefault(); if (lastParameter.Value == null) { right = "null"; _list.Remove(lastParameter.Key); } condition = string.Format("({0} {1} {2})", left, op, right); match = Regex.Match(condition, @"(.*)? = null"); if (match.Success || (match = Regex.Match(condition, "null = (.*)")).Success) { _conditionStack.Push(match.Groups[1].Value + " is null)"); } else { _conditionStack.Push(condition); } } return(node); }