示例#1
0
        /// <summary>
        /// Returns whether or not an <see cref="Expression"/> inside the given <paramref name="tree"/>
        /// matches the given <paramref name="predicate"/>.
        /// </summary>
        public static bool Exists(this Expression tree, Predicate <Expression> predicate)
        {
            bool doesExist             = false;
            ExpressionPipeline visitor = Expressive.Pipeline();

            visitor.Pipeline += expr =>
            {
                if (predicate(expr))
                {
                    doesExist = true;
                    visitor.Stop();
                }

                return(expr);
            };

            visitor.Visit(tree);

            return(doesExist);
        }
示例#2
0
        /// <summary>
        /// Returns whether or not the given expression <paramref name="tree"/>
        /// contains the specified <paramref name="expression"/>.
        /// </summary>
        public static bool Contains(this Expression tree, Expression expression)
        {
            bool doesContain           = false;
            ExpressionPipeline visitor = Expressive.Pipeline();

            visitor.Pipeline += expr =>
            {
                if (expr == expression)
                {
                    doesContain = true;
                    visitor.Stop();
                }

                return(expr);
            };

            visitor.Visit(tree);

            return(doesContain);
        }
示例#3
0
        /// <summary>
        /// Returns all <see cref="Expression"/>s that matches
        /// the given <paramref name="predicate"/>.
        /// </summary>
        public static List <Expression> FindAll(this Expression tree, Predicate <Expression> predicate)
        {
            List <Expression>  result  = new List <Expression>();
            ExpressionPipeline visitor = Expressive.Pipeline();

            visitor.Pipeline += expr =>
            {
                if (predicate(expr))
                {
                    result.Add(expr);
                    visitor.Stop();
                }

                return(expr);
            };

            visitor.Visit(tree);

            return(result);
        }
示例#4
0
        /// <summary>
        /// Returns the first <see cref="Expression"/> that matches
        /// the given <paramref name="predicate"/>.
        /// </summary>
        public static Expression Find(this Expression tree, Predicate <Expression> predicate)
        {
            Expression         result  = null;
            ExpressionPipeline visitor = Expressive.Pipeline();

            visitor.Pipeline += expr =>
            {
                if (predicate(expr))
                {
                    result = expr;
                    visitor.Stop();
                }

                return(expr);
            };

            visitor.Visit(tree);

            return(result);
        }
示例#5
0
 /// <summary>
 /// Returns the given expression <paramref name="tree"/>, replacing
 /// one expression by another.
 /// </summary>
 public static Expression Replace(this Expression tree, Expression replacee, Expression replacer)
 {
     return(Expressive.Pipeline(expr => expr == replacee ? replacer : expr).Visit(tree));
 }