Example #1
0
        public static Func <T, T> Compile <T>(this XList <Expression <Func <T, T> > > expressions)
        {
            if (expressions.IsNullOrEmpty())
            {
                return((p) => p);
            }

            if (expressions.Count == 1)
            {
                return(expressions[0].Compile());
            }

            // return expressions.Aggregate<Expression<Func<T, T>>, Expression<Func<T, T>>>(p => expressions[0].Compile(p), (current, v) => current.ThenCompile(v)).Compile()
            var resultExpression = expressions[0];

            foreach (var ex in expressions.Skip(1))
            {
                Expression <Func <T, T> > currentExpression = resultExpression;
                resultExpression = p => ex.Compile()(currentExpression.Compile()(p));
            }
            return(resultExpression.Compile());
        }