System.Linq.Expressions.Expression.Invoke C# (CSharp) Method

Invoke() public static method

Creates an InvocationExpression that applies a delegate or lambda expression to a list of argument expressions.
/// is null. /// .Type does not represent a delegate type or an .-or-The property of an element of is not assignable to the type of the corresponding parameter of the delegate represented by . /// does not contain the same number of elements as the list of parameters for the delegate represented by .
public static Invoke ( Expression expression, IEnumerable arguments ) : InvocationExpression
expression Expression /// An that represents the delegate /// or lambda expression to be applied. ///
arguments IEnumerable /// An of objects /// that represent the arguments that the delegate or lambda expression is applied to. ///
return InvocationExpression
        public static InvocationExpression Invoke(Expression expression, IEnumerable<Expression> arguments)
        {
            IReadOnlyList<Expression> argumentList = arguments as IReadOnlyList<Expression> ?? arguments.ToReadOnly();

            switch (argumentList.Count)
            {
                case 0:
                    return Invoke(expression);
                case 1:
                    return Invoke(expression, argumentList[0]);
                case 2:
                    return Invoke(expression, argumentList[0], argumentList[1]);
                case 3:
                    return Invoke(expression, argumentList[0], argumentList[1], argumentList[2]);
                case 4:
                    return Invoke(expression, argumentList[0], argumentList[1], argumentList[2], argumentList[3]);
                case 5:
                    return Invoke(expression, argumentList[0], argumentList[1], argumentList[2], argumentList[3], argumentList[4]);
            }

            RequiresCanRead(expression, nameof(expression));

            ReadOnlyCollection<Expression> args = argumentList.ToReadOnly(); // Ensure is TrueReadOnlyCollection when count > 5. Returns fast if it already is.
            MethodInfo mi = GetInvokeMethod(expression);
            ValidateArgumentTypes(mi, ExpressionType.Invoke, ref args, nameof(expression));
            return new InvocationExpressionN(expression, args, mi.ReturnType);
        }

Same methods

Expression::Invoke ( Expression expression ) : InvocationExpression
Expression::Invoke ( Expression expression, Expression arg0 ) : InvocationExpression
Expression::Invoke ( Expression expression, Expression arg0, Expression arg1 ) : InvocationExpression
Expression::Invoke ( Expression expression, Expression arg0, Expression arg1, Expression arg2 ) : InvocationExpression
Expression::Invoke ( Expression expression, Expression arg0, Expression arg1, Expression arg2, Expression arg3 ) : InvocationExpression
Expression::Invoke ( Expression expression, Expression arg0, Expression arg1, Expression arg2, Expression arg3, Expression arg4 ) : InvocationExpression

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Creates an expression that defines a logical AND function
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public static Expression <Func <T, bool> > And <T>(this Expression <Func <T, bool> > left, Expression <Func <T, bool> > right)
        {
            var invokedExpr = XPR.Invoke(right, left.Parameters.Cast <XPR>());

            return(XPR.Lambda <Func <T, bool> >
                       (XPR.AndAlso(left.Body, invokedExpr), left.Parameters));
        }
All Usage Examples Of System.Linq.Expressions.Expression::Invoke
Expression