Ejemplo n.º 1
0
        public object Resolve(Type type, string code, IDictionary <string, object> param)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new ArgumentNullException(nameof(code));
            }
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            var optionsValue = this.options.Value;

            if (optionsValue.IsReplaceOperator)
            {
                code = this.codeCaches.GetOrAdd(code, (key) => this.ReplaceOperator(code));
            }

            var engine = new Engine(options =>
            {
                options.DebugMode(false);

                if (optionsValue.IsConvertEnum)
                {
                    options.AddObjectConverter(JavaScriptEnumConverter.Instance);
                }
            });

            foreach (var item in param)
            {
                engine.SetValue(item.Key, item.Value);
            }
            ;

            engine.Execute(code);

            var value = engine.GetCompletionValue();

            var result = value.ToObject();

            if (result == null)
            {
                return(null);
            }

            return(Convert.ChangeType(result, type));
        }