Esempio n. 1
0
        public static LambdaParser.CompiledExpression CompileExpression(this NReco.Linq.LambdaParser parser, string stringExpression)
        {
            var expression = parser.Parse(stringExpression);

            var compiledExpr = new LambdaParser.CompiledExpression()
            {
                Parameters = LambdaParser.GetExpressionParameters(expression)
            };

            var lambdaExpr = Expression.Lambda(expression, compiledExpr.Parameters);

            compiledExpr.Lambda = lambdaExpr.Compile();

            return(compiledExpr);
        }
Esempio n. 2
0
        public static object Evaluate(this LambdaParser.CompiledExpression compiledExpression, Dictionary <string, object> values)
        {
            var valuesList = new List <object>();

            foreach (var paramExpr in compiledExpression.Parameters)
            {
                valuesList.Add(new LambdaParameterWrapper(values[paramExpr.Name]));
            }

            var lambdaRes = compiledExpression.Lambda.DynamicInvoke(valuesList.ToArray());

            if (lambdaRes is NReco.Linq.LambdaParameterWrapper)
            {
                return(((NReco.Linq.LambdaParameterWrapper)lambdaRes).Value);
            }

            return(lambdaRes);
        }