コード例 #1
0
        protected override Expression VisitBinary(BinaryExpression binaryExpression)
        {
            var left  = this.Visit(binaryExpression.Left);
            var right = this.Visit(binaryExpression.Right);

            if (left.Type == binaryExpression.Left.Type && right.Type == binaryExpression.Right.Type)
            {
                return(binaryExpression.ChangeLeftRight(left, right));
            }

            if (!this.IsTaskType(left.Type) && !this.IsTaskType(right.Type))
            {
                return(binaryExpression.ChangeLeftRight(left, right));
            }

            if (this.IsTaskType(left.Type) && this.IsTaskType(right.Type))
            {
                var leftVar   = Expression.Parameter(left.Type);
                var rightVar  = Expression.Parameter(right.Type);
                var variables = new[] { leftVar, rightVar };

                var expression = CreateChainedCalls(leftVar, rightVar);
                var lambda     = Expression.Lambda(binaryExpression.ChangeLeftRight(Expression.Property(leftVar, "Result"), Expression.Property(rightVar, "Result")), Expression.Parameter(expression.Type));

                return(Expression.Block(variables, Expression.Assign(leftVar, left), Expression.Assign(rightVar, right), Expression.Call(expression, "ContinueWith", new[] { lambda.ReturnType }, lambda)));
            }

            if (this.IsTaskType(left.Type))
            {
                var leftVar   = Expression.Parameter(left.Type);
                var variables = new[] { leftVar };

                var expression = CreateChainedCalls(leftVar);
                var lambda     = Expression.Lambda(binaryExpression.ChangeLeftRight(Expression.Property(leftVar, "Result"), right), Expression.Parameter(expression.Type));

                return(Expression.Block(variables, Expression.Assign(leftVar, left), Expression.Call(expression, "ContinueWith", new[] { lambda.ReturnType }, lambda)));
            }

            if (this.IsTaskType(right.Type))
            {
                var rightVar  = Expression.Parameter(right.Type);
                var variables = new[] { rightVar };

                var expression = CreateChainedCalls(rightVar);
                var lambda     = Expression.Lambda(binaryExpression.ChangeLeftRight(left, Expression.Property(rightVar, "Result")), Expression.Parameter(expression.Type));

                return(Expression.Block(variables, Expression.Assign(rightVar, right), Expression.Call(expression, "ContinueWith", new[] { lambda.ReturnType }, lambda)));
            }

            return(base.VisitBinary(binaryExpression));
        }