コード例 #1
0
        public void VisitGreaterThan(BinaryExpression expr_)
        {
            Type           expType = expr_.GetType();
            Type           type_   = expr_.GetType().BaseType;
            ExpressionType ndType  = expr_.NodeType;

            Visit(expr_.Left);
            cb.GreaterThan();
            Visit(expr_.Right);
        }
コード例 #2
0
        public void VisitEqual(BinaryExpression expr_)
        {
            Type           expType = expr_.GetType();
            Type           type_   = expr_.GetType().BaseType;
            ExpressionType ndType  = expr_.NodeType;

            Visit(expr_.Left);
            cb.EqualSign();
            Visit(expr_.Right);
        }
コード例 #3
0
    public Expression WithLog(BinaryExpression exp)
    {
        var subLambda = Expression.Lambda(
            Expression.Convert(exp, typeof(object)),
            _parameters
            );
        var subFunc = subLambda.Compile();
        var result  = subFunc.DynamicInvoke(_parameterValues);

        return(Expression.Block(
                   Expression.Call(
                       typeof(Debug).GetMethod("Print", new Type[] { typeof(string) }),
                       new[]
        {
            Expression.Call(
                typeof(string).GetMethod("Format", new [] { typeof(string), typeof(object), typeof(object) }),
                Expression.Constant("Executing Rule: {0} --> {1}"),
                Expression.Call(Expression.Constant(exp), exp.GetType().GetMethod("ToString")),
                Expression.Convert(Expression.Constant(result), typeof(object))
                )
        }
                       ),
                   base.VisitBinary(exp)
                   ));
    }
コード例 #4
0
ファイル: Program.cs プロジェクト: zuojiashun/ajcodekatas
        static void Main(string[] args)
        {
            BinaryExpression expr = Expression.Add(Expression.Constant(10),
                                                   Expression.Constant(20));

            Console.WriteLine(expr.GetType().FullName);

            Console.WriteLine(expr.Type);
            Console.WriteLine(expr.Left.Type);
            Console.WriteLine(expr.Right.Type);

            Console.WriteLine(expr.NodeType);
            Console.WriteLine(expr.Left.NodeType);
            Console.WriteLine(expr.Right.NodeType);

            // double result = (3d + 2d) / 7d;
            // Console.WriteLine(result);

            BinaryExpression expr2 = Expression.Divide(
                Expression.Add(Expression.Constant(3d),
                               Expression.Constant(2d)),
                Expression.Constant(7d)
                );

            Func <double> binaryDelegate = Expression.Lambda <Func <double> >(expr2).Compile();

            Console.WriteLine(binaryDelegate.Invoke());
        }
コード例 #5
0
 //
 // Summary:
 //     Visits the children of the System.Linq.Expressions.BinaryExpression.
 //
 // Parameters:
 //   node:
 //     The expression to visit.
 //
 // Returns:
 //     The modified expression, if it or any subexpression was modified; otherwise,
 //     returns the original expression.
 protected override Expression VisitBinary(BinaryExpression node)
 {
     Console.WriteLine("VisitBinary:");
     Console.WriteLine('\t' + node.GetType().ToString());
     Console.WriteLine('\t' + node.ToString());
     return(base.VisitBinary(node));
 }
コード例 #6
0
 protected override void Visit(BinaryExpression node)
 {
     InsideTag("BinaryExpression", () =>
     {
         this.InsideTag("Left", () => { this.VisitNode(node.Left); });
         this.InsideTag("Op", () => { Out.WriteLine(node.GetType().ToString()); });
         this.InsideTag("Right", () => { this.VisitNode(node.Right); });
     });
 }
コード例 #7
0
        private static QueryFilter ParseComparison <T>(BinaryExpression binaryExpression, ParameterExpression parameter)
        {
            var leftIsConstant  = ExpressionHelper.TryConvertToConstant(binaryExpression.Left, out var leftConstant);
            var rightIsConstant = ExpressionHelper.TryConvertToConstant(binaryExpression.Right, out var rightConstant);
            var leftIsProperty  = TryConvertToMember(binaryExpression.Left, out var leftMember);
            var rightIsProperty = TryConvertToMember(binaryExpression.Right, out var rightMember);

            if (!((leftIsConstant ^ rightIsConstant) || (leftIsProperty || rightIsProperty)))
            {
                throw new Exception($"{nameof(Parse)}: Expressions of type {binaryExpression.GetType()} with {nameof(binaryExpression.NodeType)}={binaryExpression.NodeType} must contain an expression that compiles to a constant value and a member expression. Found (sub-)expression: '{binaryExpression}'.");
            }

            var constantValue    = leftIsConstant ? leftConstant : rightConstant;
            var memberExpression = leftIsConstant ? rightMember : leftMember;

            var property = QueryInfo.EncodeMemberExpression(memberExpression, parameter);
            var constant = EncodeConstant(constantValue, constantValue.GetType());

            switch (binaryExpression.NodeType)
            {
            case ExpressionType.GreaterThan:
                return(GreaterThan(property, constant));

            case ExpressionType.GreaterThanOrEqual:
                return(GreaterThanEqual(property, constant));

            case ExpressionType.LessThan:
                return(LessThan(property, constant));

            case ExpressionType.LessThanOrEqual:
                return(LessThanEqual(property, constant));

            case ExpressionType.Equal:
                return(Exact(property, constant));

            default:
                throw new Exception($"{nameof(Parse)}: Expressions of type {binaryExpression.GetType()} with {nameof(binaryExpression.NodeType)}={binaryExpression.NodeType} are not supported yet. Found (sub-)expression: {binaryExpression}");
            }
        }
コード例 #8
0
 private static int PrintBinary(BinaryExpression expression)
 {
     Write("ToString: " + expression);
     Write("GetType: " + expression.GetType());
     Write("Conversion: " + expression.Conversion);
     Write("IsLifted: " + expression.IsLifted);
     Write("IsLiftedToNull: " + expression.IsLiftedToNull);
     Write("Left: " + expression.Left);
     PrintUnkwnown(expression.Left);
     Write("Method: " + expression.Method);
     Write("NodeType: " + expression.NodeType);
     Write("Right: " + expression.Right);
     PrintUnkwnown(expression.Right);
     Write("Type: " + expression.Type);
     return(0);
 }
コード例 #9
0
        internal static bool IsStringConcat(BinaryExpression binaryExpression)
        {
            var methodProperty = binaryExpression.GetType().GetProperty("Method");
            if (methodProperty == null)
            {
                return false;
            }

            var method = methodProperty.GetValue(binaryExpression) as MethodInfo;
            if (method == null)
            {
                return false;
            }

            return method.DeclaringType == typeof(string) && method.Name == nameof(string.Concat);
        }
コード例 #10
0
 public Expression WithLog(BinaryExpression exp)
 {
     return(Expression.Block(
                Expression.Call(
                    typeof(Debug).GetMethod("Print", new Type[] { typeof(string) }),
                    new[]
     {
         Expression.Call(
             typeof(string).GetMethod("Format", new [] { typeof(string), typeof(object), typeof(object) }),
             Expression.Constant("Executing Rule: {0} --> {1}"),
             Expression.Call(Expression.Constant(exp), exp.GetType().GetMethod("ToString")),
             Expression.Convert(
                 exp,
                 typeof(object)
                 )
             )
     }
                    ),
                base.VisitBinary(exp)
                ));
 }
コード例 #11
0
        protected override Expression VisitBinary(BinaryExpression node)
        {
            bool isChecked = false;

            switch (node.NodeType)
            {
            case ExpressionType.AddAssignChecked:
            case ExpressionType.AddChecked:
            case ExpressionType.ConvertChecked:
            case ExpressionType.MultiplyAssignChecked:
            case ExpressionType.MultiplyChecked:
            case ExpressionType.SubtractAssignChecked:
            case ExpressionType.SubtractChecked:
            {
                isChecked = true;
                break;
            }
            }

            if (isChecked)
            {
                this._writer.Write("checked( ");
            }

            this.Visit(node.Left);

            switch (node.NodeType)
            {
            case ExpressionType.Add:
            case ExpressionType.AddChecked:
            {
                this._writer.Write(" + ");
                break;
            }

            case ExpressionType.AddAssign:
            case ExpressionType.AddAssignChecked:
            {
                this._writer.Write(" += ");
                break;
            }

            case ExpressionType.And:
            {
                this._writer.Write(" & ");
                break;
            }

            case ExpressionType.AndAlso:
            {
                this._writer.Write(" && ");
                break;
            }

            case ExpressionType.AndAssign:
            {
                this._writer.Write(" &= ");
                break;
            }

            case ExpressionType.Assign:
            {
                this._writer.Write(" = ");
                break;
            }

            case ExpressionType.ArrayIndex:
            {
                this._writer.Write("[ ");
                break;
            }

            case ExpressionType.Coalesce:
            {
                this._writer.Write(" ?? ");
                break;
            }

            case ExpressionType.Divide:
            {
                this._writer.Write(" / ");
                break;
            }

            case ExpressionType.DivideAssign:
            {
                this._writer.Write(" /= ");
                break;
            }

            case ExpressionType.Equal:
            {
                this._writer.Write(" == ");
                break;
            }

            case ExpressionType.ExclusiveOr:
            {
                this._writer.Write(" ^ ");
                break;
            }

            case ExpressionType.ExclusiveOrAssign:
            {
                this._writer.Write(" ^= ");
                break;
            }

            case ExpressionType.GreaterThan:
            {
                this._writer.Write(" > ");
                break;
            }

            case ExpressionType.GreaterThanOrEqual:
            {
                this._writer.Write(" >= ");
                break;
            }

            case ExpressionType.LeftShift:
            {
                this._writer.Write(" << ");
                break;
            }

            case ExpressionType.LeftShiftAssign:
            {
                this._writer.Write(" <<= ");
                break;
            }

            case ExpressionType.LessThan:
            {
                this._writer.Write(" < ");
                break;
            }

            case ExpressionType.LessThanOrEqual:
            {
                this._writer.Write(" <= ");
                break;
            }

            case ExpressionType.Modulo:
            {
                this._writer.Write(" % ");
                break;
            }

            case ExpressionType.ModuloAssign:
            {
                this._writer.Write(" %= ");
                break;
            }

            case ExpressionType.Multiply:
            case ExpressionType.MultiplyChecked:
            {
                this._writer.Write(" * ");
                break;
            }

            case ExpressionType.MultiplyAssign:
            case ExpressionType.MultiplyAssignChecked:
            {
                this._writer.Write(" *= ");
                break;
            }

            case ExpressionType.NotEqual:
            {
                this._writer.Write(" != ");
                break;
            }

            case ExpressionType.Or:
            {
                this._writer.Write(" | ");
                break;
            }

            case ExpressionType.OrAssign:
            {
                this._writer.Write(" |= ");
                break;
            }

            case ExpressionType.OrElse:
            {
                this._writer.Write(" || ");
                break;
            }

            case ExpressionType.Power:
            {
                this._writer.Write(" `pow` ");
                break;
            }

            case ExpressionType.PowerAssign:
            {
                this._writer.Write(" `pow`= ");
                break;
            }

            case ExpressionType.RightShift:
            {
                this._writer.Write(" >> ");
                break;
            }

            case ExpressionType.RightShiftAssign:
            {
                this._writer.Write(" >>= ");
                break;
            }

            case ExpressionType.Subtract:
            case ExpressionType.SubtractChecked:
            {
                this._writer.Write(" - ");
                break;
            }

            case ExpressionType.SubtractAssign:
            case ExpressionType.SubtractAssignChecked:
            {
                this._writer.Write(" -= ");
                break;
            }

            default:
            {
                throw new NotSupportedException(String.Format(CultureInfo.CurrentCulture, "Binary operation {0}(NodeType:{1}) is not supported. Expression tree:{2}", node.GetType().Name, node.NodeType, node));
            }
            }

            this.Visit(node.Right);

            if (isChecked)
            {
                this._writer.Write(" )");
            }
            else if (node.NodeType == ExpressionType.ArrayIndex)
            {
                this._writer.Write(" ]");
            }

            return(node);
        }
コード例 #12
0
ファイル: Visitor.cs プロジェクト: thibautdavid/netpatterns
 public void Visit(BinaryExpression expression)
 {
     expression.Left.Accept(this);
     _aggregate.Append($" {expression.GetType().GetCustomAttribute<OperatorAttribute>().OperatorSign} ");
     expression.Right.Accept(this);
 }
コード例 #13
0
ファイル: SecondPass.cs プロジェクト: goric/tranche-net
        private void VisitBinaryArithmetic(BinaryExpression n)
        {
            var lhs = CheckSubTree(n.Left);
            var rhs = CheckSubTree(n.Right);

            if (lhs.IsNumeric && rhs.IsNumeric)
            {
                _lastSeenType = n.InternalType = Supertype(lhs, rhs);
            }
            else if (lhs.CilType == typeof(DateTime) && rhs.CilType == typeof(DateTime))
            {
                _lastSeenType = n.InternalType = new TypeSpan();
            }
            else
            {
                ReportError(n.Location, "Invalid operands for operation {0}. Got types '{1}' and '{2}'.", n.GetType().ToString(), TypeToFriendlyName(lhs), TypeToFriendlyName(rhs));
            }
        }