Esempio n. 1
0
        Object CalcSimpleExpression(String expression)
        {
            var lexpr  = DynamicParser.ParseLambda(null, expression);
            var lambda = lexpr.Compile();

            return(lambda.DynamicInvoke());
        }
        public T CalcExpression <T>(ExpandoObject root, String expression)
        {
            Object result;

            if (_lambdas != null && _lambdas.TryGetValue(expression, out Delegate expr))
            {
                result = expr.DynamicInvoke(root);
            }
            else
            {
                if (_lambdas == null)
                {
                    _lambdas = new Dictionary <String, Delegate>();
                }
                var prms = new ParameterExpression[] {
                    Expression.Parameter(typeof(ExpandoObject), "Root")
                };
                var lexpr = DynamicParser.ParseLambda(prms, expression);
                expr = lexpr.Compile();
                _lambdas.Add(expression, expr);
                result = expr.DynamicInvoke(root);
            }
            if (result == null)
            {
                return(default);
Esempio n. 3
0
        Object CalcExpression(String expression, String prm, Object value)
        {
            var prms = new ParameterExpression[] {
                Expression.Parameter(typeof(Object), prm)
            };
            var lexpr  = DynamicParser.ParseLambda(prms, expression);
            var lambda = lexpr.Compile();

            return(lambda.DynamicInvoke(value));
        }
Esempio n. 4
0
        public T CalcExpression <T>(ExpandoObject root, String expression)
        {
            Object result = null;

            if (_lambdas != null && _lambdas.TryGetValue(expression, out Delegate expr))
            {
                result = expr.DynamicInvoke(root);
            }
            else
            {
                if (_lambdas == null)
                {
                    _lambdas = new Dictionary <String, Delegate>();
                }
                var prms = new ParameterExpression[] {
                    Expression.Parameter(typeof(ExpandoObject), "Root")
                };
                var lexpr = DynamicParser.ParseLambda(prms, expression);
                expr = lexpr.Compile();
                _lambdas.Add(expression, expr);
                result = expr.DynamicInvoke(root);
            }
            if (result == null)
            {
                return(default(T));
            }
            if (result is T resultT)
            {
                return(resultT);
            }
            var tp = typeof(T);

            if (tp.IsNullableType())
            {
                tp = Nullable.GetUnderlyingType(tp);
            }
            return((T)Convert.ChangeType(result, tp));
        }