Update() public method

Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will return this expression.
public Update ( Expression test, Expression ifTrue, Expression ifFalse ) : ConditionalExpression
test Expression The property of the result.
ifTrue Expression The property of the result.
ifFalse Expression The property of the result.
return ConditionalExpression
示例#1
0
        /// <summary>
        ///     Visits the children of the <see cref="ConditionalExpression" />.
        /// </summary>
        /// <param name="node">The expression to visit.</param>
        /// <returns>
        ///     The modified expression, if it or any subexpression was modified;
        ///     otherwise, returns the original expression.
        /// </returns>
        protected internal virtual Expression VisitConditional(ConditionalExpression node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            return(node.Update(Visit(node.Test), Visit(node.IfTrue), Visit(node.IfFalse)));
        }
示例#2
0
        protected override ExpressionTree VisitConditional(ConditionalExpression node)
        {
            CheckChildrenCount(3);

            var t = ExtractChildExpression(0);
            var p = ExtractChildExpression(1);
            var n = ExtractChildExpression(2);

            var expression = node.Update(t, p, n);

            return(CreateExpressionTree(expression));
        }
示例#3
0
 /// <summary>
 /// Visits the children of the <see cref="ConditionalExpression"/>.
 /// </summary>
 /// <param name="node">The expression to visit.</param>
 /// <returns>The modified expression, if it or any subexpression was modified;
 /// otherwise, returns the original expression.</returns>
 protected internal virtual Expression VisitConditional(ConditionalExpression node)
 {
     return(node.Update(Visit(node.Test), Visit(node.IfTrue), Visit(node.IfFalse)));
 }
        protected override Expression VisitConditional(ConditionalExpression node)
        {
            Debug.WriteLine(node);
            var ifTrue = Visit(node.IfTrue);
            var ifFalse = Visit(node.IfFalse);
            var test = Visit(node.Test);

            Expression expression;
            if (ExtractNullableArgument(test, ifTrue, out expression))
            {
                return Expression.Coalesce(expression, ifFalse);
            }
            var ifTrueBinary = UnwrapConvertExpression(ifTrue) as BinaryExpression;
            if (ifTrueBinary != null)
            {
                BinaryExpression result;
                if (TryConvert1(test, ifTrueBinary, out result))
                    return result;
            }
            
            var binaryExpression = test as BinaryExpression;
            var ifTrueConstant = ifTrue as ConstantExpression;
            var ifFalseConstant = ifFalse as ConstantExpression;

            if (binaryExpression != null)
            {
                BinaryExpression result;
                if (ifTrueBinary != null && TryConvert2(binaryExpression, ifTrueBinary, out result))
                {
                    return result;
                }
                if (TryConvert(ifTrueConstant, binaryExpression, ifFalse, out result, false))
                {
                    return result;
                }
                if (TryConvert(ifFalseConstant, binaryExpression, ifTrue, out result, true))
                {
                    return result;
                }
                if (binaryExpression.NodeType == ExpressionType.NotEqual)
                {
                    if (binaryExpression.Left == node.IfTrue)
                    {
                        if (binaryExpression.Right is DefaultExpression)
                        {
                            return Expression.Coalesce(binaryExpression.Left, ifFalse);
                        }
                        var rightConstant = binaryExpression.Right as ConstantExpression;
                        if (rightConstant != null && rightConstant.Value == null)
                        {
                            return Expression.Coalesce(binaryExpression.Left, ifFalse);
                        }
                    }
                }
            }

            if (test.NodeType == ExpressionType.Not)
            {
                if (ifTrueConstant != null)
                {
                    if (ifTrueConstant.Value as bool? == false)
                    {
                        return Expression.AndAlso(((UnaryExpression) test).Operand, ifFalse);
                    }
                }
            }

            return node.Update(test, ifTrue, ifFalse);
        }
示例#5
0
#pragma warning restore CA1810
#pragma warning restore IDE0079

        /// <summary>
        /// Visits the conditional expression and avoids inadvertening cloning of the node when unchanged due to a bug in the .NET Framework.
        /// </summary>
        /// <param name="node">The expression to visit.</param>
        /// <returns>The modified expression, if it or any subexpression was modified; otherwise, returns the original expression.</returns>
        /// <remarks>
        /// Works around a bug in ConditionalExpression.Update that causes inadvertent cloning of `IfThen` nodes.
        /// See https://github.com/dotnet/corefx/issues/3223 for more information.
        /// </remarks>
        protected override Expression VisitConditional(ConditionalExpression node)
        {
            //
            // No need to do some workaround if the quirk has been fixed.
            //
            if (s_hasIfThenQuirk)
            {
                //
                // The quirk exists, so only access IfFalse once to avoid triggering allocations
                // of spurious Default(typeof(void)) nodes.
                //
                var oldIfFalse = node.IfFalse;

                //
                // See ConditionalExpression.Make for the logic that leads to special-casing nodes
                // whose IfFalse child is a DefaultExpression of type void.
                //
                if (node.Type == typeof(void) && IsEmpty(oldIfFalse))
                {
                    var oldTest   = node.Test;
                    var oldIfTrue = node.IfTrue;

                    //
                    // Visit in the same order as the base class would do. We still visit IfFalse,
                    // which always returns a new instance of Default(typeof(void)) when the quirk
                    // is present. This is needed because a visitor subtype may want to do override
                    // VisitDefault and change the IfFalse node.
                    //
                    var newTest    = Visit(oldTest);
                    var newIfTrue  = Visit(oldIfTrue);
                    var newIfFalse = Visit(oldIfFalse);

                    //
                    // Check child reference equality to avoid cloning, as we normally would do,
                    // but check the case where the new IfFalse child node is Default(typeof(void))
                    // which we will classify as no change either.
                    //
                    // NB: In case the user rewrites the expression tree to ensure uniqueness of
                    //     references of nodes (i.e. intentional cloning), it's fine to keep the
                    //     node as-is, because ConditionalExpression's behavior of returning a new
                    //     instance of Default(typeof(void)) for each access to IfFalse ensures
                    //     the same uniqueness property. Stated otherwise, the unique Default node
                    //     handed to us by such a user doesn't get stored in ConditionalExpression
                    //     anyway, in favor of GetFalse() returning new Default(typeof(void)) nodes
                    //     for each access to IfFalse.
                    //
                    if (newTest == oldTest &&
                        newIfTrue == oldIfTrue &&
                        (newIfFalse == oldIfFalse || IsEmpty(newIfFalse)))
                    {
                        return(node);
                    }

                    //
                    // If anything changed for real, call Update as usual.
                    //
                    return(node.Update(newTest, newIfTrue, newIfFalse));
                }
            }

            return(base.VisitConditional(node));
        }
        protected override Expression VisitConditional(ConditionalExpression node)
        {
            var children = new[] {node.Test, node.IfTrue, node.IfFalse};

            VisitChildren(children);

            return node.Update(children[0], children[1], children[2]);
        }
 private ConditionalExpression replaceConditionalNodeChild(ConditionalExpression conditionalExpression, Expression oldChild, Expression newChild)
 {
     Expression test = conditionalExpression.Test.Equals(oldChild) ? newChild : conditionalExpression.Test;
     Expression ifTrue = conditionalExpression.IfTrue.Equals(oldChild) ? newChild : conditionalExpression.IfTrue;
     Expression ifFalse = conditionalExpression.IfFalse.Equals(oldChild) ? newChild : conditionalExpression.IfFalse;
     return conditionalExpression.Update(test, ifTrue, ifFalse);
 }