Exemple #1
0
        public static object EvaluateUntypedExpression(string p_expression, IParameterResolver p_resolver, IAttributeDataCollection p_collection, bool p_referenced)
        {
            hasErrorInEvaluation = false;
            if (_cachedExpressions == null)
            {
                _cachedExpressions = new Dictionary <string, Expression>();
            }

            Expression cachedExpression;

            if (!_cachedExpressions.ContainsKey(p_expression))
            {
                cachedExpression = new Expression(p_expression);
                _cachedExpressions.Add(p_expression, cachedExpression);
            }
            else
            {
                cachedExpression = _cachedExpressions[p_expression];
            }

            EvaluateFunctionHandler evalFunction = (name, args) => EvaluateFunction(name, args);

            cachedExpression.EvaluateFunction += evalFunction;
            EvaluateParameterHandler evalParam = (name, args) => EvaluateParameter(name, args, p_resolver, p_collection, p_referenced);

            cachedExpression.EvaluateParameter += evalParam;

            object obj = null;

            try
            {
                obj = cachedExpression.Evaluate();
            }
            catch (Exception e)
            {
                // Only set if we didn't already encounter error in evaluation otherwise this may be unspecified exception as a result of the already logged error so we don't want to overwrite it
                if (!hasErrorInEvaluation)
                {
                    errorMessage         = e.Message;
                    hasErrorInEvaluation = true;
                }
            }

            cachedExpression.EvaluateFunction  -= evalFunction;
            cachedExpression.EvaluateParameter -= evalParam;

            return(obj);
        }
Exemple #2
0
        public static T EvaluateExpression <T>(string p_expression, IParameterResolver p_resolver, IAttributeDataCollection p_collection, bool p_referenced)
        {
            hasErrorInEvaluation = false;
            if (_cachedExpressions == null)
            {
                _cachedExpressions = new Dictionary <string, Expression>();
            }

            Expression cachedExpression;

            if (!_cachedExpressions.ContainsKey(p_expression))
            {
                // We cache before macro replacement so runtime macro changes are not possible for performance reasons
                cachedExpression = new Expression(ReplaceMacros(p_expression));
                _cachedExpressions.Add(p_expression, cachedExpression);
            }
            else
            {
                cachedExpression = _cachedExpressions[p_expression];
            }

            EvaluateFunctionHandler evalFunction = (name, args) => EvaluateFunction <T>(name, args);

            cachedExpression.EvaluateFunction += evalFunction;
            EvaluateParameterHandler evalParam = (name, args) => EvaluateParameter(name, args, p_resolver, p_collection, p_referenced);

            cachedExpression.EvaluateParameter += evalParam;

            object obj = null;

            //try
            {
                obj = cachedExpression.Evaluate();
            }
            // catch (Exception e)
            // {
            //     // Only set if we didn't already encounter error in evaluation otherwise this may be unspecified exception as a result of the already logged error so we don't want to overwrite it
            //     if (!hasErrorInEvaluation)
            //     {
            //         errorMessage = e.Message;
            //         hasErrorInEvaluation = true;-+
            //     }
            // }

            cachedExpression.EvaluateFunction  -= evalFunction;
            cachedExpression.EvaluateParameter -= evalParam;

            if (obj != null)
            {
                Type returnType = obj.GetType();
                if (typeof(T).IsAssignableFrom(returnType))
                {
                    return((T)obj);
                }

                // Explicit numeric type casting at cost of precision/overflow
                if (typeof(T).IsNumericType() && returnType.IsNumericType())
                {
                    return((T)Convert.ChangeType(obj, typeof(T)));
                }

                if (typeof(T).IsImplicitlyAssignableFrom(returnType))
                {
                    return((T)Convert.ChangeType(obj, typeof(T)));
                }

                if (typeof(T) == typeof(string))
                {
                    return((T)(object)obj.ToString());
                }

                Debug.LogWarning("Invalid expression casting " + obj.GetType() + " and " + typeof(T));
            }

            return(default(T));
        }