public Expression GetGetterExpression(ParameterExpression parameterExpr)
        {
            // TODO does this keep swapping the same parameter?
            var replacer = new ParameterReplacerVisitor(_getterExpr.Parameters[0], parameterExpr);

            return(replacer.Visit(_getterExpr.Body));
        }
        public bool TryParse(PrioritizedString expr, ParameterInfo paramInfo, out Expression parsed)
        {
            var minPriorityInds = expr.Priorities.GetMinIndexes();
            var whereParts      = expr.SplitOnSubset("where", minPriorityInds);

            if (whereParts.Length > 2)
            {
                throw new ParseException("Too many 'where'!");
            }

            if (whereParts.Length == 1)
            {
                parsed = null;
                return(false);
            }

            var mainFunc = whereParts[0];

            if (mainFunc.Input.Last() == ',')
            {
                mainFunc = mainFunc.Substring(0, mainFunc.Input.Length - 1);
            }

            var parameterExpressions = new Dictionary <string, PrioritizedString>();

            foreach (Match match in ParameterRe.Matches(whereParts[1].Input))
            {
                var(paramName, paramExpr) = (match.Groups["paramName"].Value, match.Groups["paramExpr"].Value);
                if (paramInfo.Parameters.ContainsKey(paramName))
                {
                    throw new ParseException($"Parameter {paramName} is defined multiple times");
                }

                paramInfo.Parameters[paramName] = Expression.Parameter(typeof(double), paramName);
                parameterExpressions[paramName] = new PrioritizedString(paramExpr);
            }
            var parameterExprs = parameterExpressions
                                 .ToDictionary(
                nameWithExpr => nameWithExpr.Key,
                nameWithExpr => Combinator.ParseFunctionalExpression(nameWithExpr.Value, paramInfo)
                );
            var mainExpr = Combinator.ParseFunctionalExpression(mainFunc, paramInfo);

            var paramReplacer = new ParameterReplacerVisitor(parameterExprs);

            parsed = paramReplacer.Visit(mainExpr);
            return(true);
        }
        public EquatableExpression(LambdaExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (expression.Parameters.Count != 1)
            {
                throw new ArgumentException("Invalid LambdaExpression. Must only have one parameter.");
            }
            _expression    = expression;
            _parameterType = _expression.Parameters[0].Type;
            var visitor = new ParameterReplacerVisitor();

            _expressionAsString = visitor.Visit(expression).ToString();
        }
Example #4
0
        public static Expression <Func <T, TResult> > Chain <T, TIntermediate, TResult>(this Expression <Func <T, TIntermediate> > expression, Expression <Func <TIntermediate, TResult> > otherExpression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (otherExpression == null)
            {
                throw new ArgumentNullException(nameof(otherExpression));
            }

            var paramReplacer = new ParameterReplacerVisitor(expression);

            return((Expression <Func <T, TResult> >)paramReplacer.Visit(otherExpression));
        }
        internal static Expression <Func <TItem, bool> > CombinePredicates <TItem>(IEnumerable <Expression <Func <TItem, bool> > > predicates)
        {
            ParameterExpression paramExpr         = null;
            Expression          combinedPredicate = null;

            foreach (Expression <Func <TItem, bool> > predicate in predicates)
            {
                if (predicate == null)
                {
                    continue;
                }

                Expression predicateBody;

                if (paramExpr == null)
                {
                    paramExpr     = predicate.Parameters[0];
                    predicateBody = predicate.Body;
                }
                else
                {
                    var visitor = new ParameterReplacerVisitor(predicate.Parameters[0], paramExpr);
                    predicateBody = visitor.Visit(predicate.Body);
                }

                combinedPredicate = combinedPredicate == null ? predicateBody : Expression.OrElse(combinedPredicate, predicateBody);
            }

            if (combinedPredicate == null)
            {
                return(item => false);
            }

            var combinedLambda = Expression.Lambda <Func <TItem, bool> >(combinedPredicate, paramExpr);

            return(combinedLambda);
        }
        protected override Expression GetGetterExpression(ParameterExpression parameterExpr)
        {
            var replacer = new ParameterReplacerVisitor(_getterExpression.Parameters[0], parameterExpr);

            return(replacer.Visit(_getterExpression.Body));
        }
        public Expression GetSelectExpression(ParameterExpression itemPram)
        {
            var replacer = new ParameterReplacerVisitor(_middleExpression.Parameters[0], itemPram);

            return(replacer.Visit(_middleExpression.Body));
        }