コード例 #1
0
        /// <summary>
        /// Run the script and return the last value evaluated. Executing a declaration function
        /// or a global object literal, will load the function or object in the JavaScript context.
        /// </summary>
        /// <param name="script"></param>
        /// <returns>
        /// </returns>
        public object Run(string script)
        {
            var o = this._javaScriptContextImplementation.Run(script);

            if (o != null)
            {
                if (_javaScriptContextImplementation.IsJavaScriptArray(o))
                {
                    o = _javaScriptContextImplementation.JavaScriptArrayToNETArray(o);
                }
                else if (_javaScriptContextImplementation.IsJavaScriptObject(o))
                {
                    var     dic = _javaScriptContextImplementation.JavaScriptObjectToNETDictionary(o);
                    dynamic d   = new DynamicJavaScriptInstance(dic);
                    o = d;
                }
            }
            return(o);
        }
        /// <summary>
        /// Get the value of a property using the property syntax
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (!_dic.ContainsKey(binder.Name))
            {
                throw new ApplicationException(String.Format("property '{0}' not defined in object", binder.Name));
            }

            result = _dic[binder.Name];

            if (___globalJavaScriptContext.IsJavaScriptDate(result))
            {
                result = ___globalJavaScriptContext.JavaScriptDateToNETDateTime(result);
            }
            // If we have to return an array we replace all the element of the array that
            // are an object by our wrapper JavaScriptInstance around the object.
            // So we can continue to get the C# dynamic syntax
            if (___globalJavaScriptContext.IsJavaScriptArray(result))
            {
                result = MakeDynamicObjectArray(
                    ___globalJavaScriptContext.JavaScriptArrayToNETArray(result)
                    );
            }
            // If the value returned  a JavaScript object we return it as a
            // JavaScriptInstance so we can handle the dynamic aspect of it in C#
            else if (___globalJavaScriptContext.IsJavaScriptObject(result))
            {
                var     dic = ___globalJavaScriptContext.JavaScriptObjectToNETDictionary(result);
                dynamic d   = new DynamicJavaScriptInstance(dic);
                result = d;
            }
            else
            {
                result = ___globalJavaScriptContext.JavaScriptValueTypeToNETValueType(result);
            }
            return(true);
        }