Beispiel #1
0
        /// <summary>
        /// 创建JavaScript属性
        /// </summary>
        /// <param name="javascriptWrapper">经过V8 JS引擎处理后的对象</param>
        /// <param name="properties">属性键值对集合</param>
        private static void CreateJavascriptProperties(CefV8Value javascriptWrapper, IList <string> properties)
        {
            var unmanagedWrapper = (UnmanagedWrapper)(javascriptWrapper.GetUserData());

            foreach (string property in properties)
            {
                string jsPropertyName = LowercaseFirst(property);
                var    PropertyInfo   = unmanagedWrapper.ClrObject.GetType().GetProperty(property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.DeclaredOnly);
                var    v8Value        = V8ValueClrMap.ConvertToV8Value(PropertyInfo.GetValue(unmanagedWrapper.ClrObject, null));
                javascriptWrapper.SetValue(jsPropertyName, v8Value, CefV8PropertyAttribute.None);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 执行JS方法
        /// </summary>
        /// <param name="name">方法名</param>
        /// <param name="obj"></param>
        /// <param name="arguments"></param>
        /// <param name="returnValue"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            exception = null;
            try
            {
                List <object> paras  = V8ValueClrMap.ConvertToClrParameters(arguments);
                var           clrObj = V8ValueClrMap.GetClrValue(obj);
                Type          t      = null;
                if (obj.IsObject)
                {
                    t = clrObj.GetType();
                }

                var function = t.GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.IgnoreCase | BindingFlags.DeclaredOnly);
                if (function != null)
                {
                    var objRst = function.Invoke(clrObj, paras.Count == 0 ? null : paras.ToArray());
                    if (objRst is CefV8Handler)
                    {
                        returnValue = CefV8Value.CreateObject(null);
                        Bind(name, objRst as CefV8Handler, returnValue);
                    }
                    else
                    {
                        returnValue = V8ValueClrMap.ConvertToV8Value(objRst);
                    }
                }
                else
                {
                    exception   = "方法没有找到:" + name;
                    returnValue = CefV8Value.CreateString("");
                    var message3 = CefProcessMessage.Create("Exception");
                    message3.Arguments.SetString(0, exception);
                    //var success2 = Browser.SendProcessMessage(CefProcessId.Browser, message3);

                    return(true);
                }
                return(true);
            }
            catch (Exception ee)
            {
                returnValue = CefV8Value.CreateString("");
                exception   = ee.Message;

                returnValue = CefV8Value.CreateString("");
                var message3 = CefProcessMessage.Create("Exception");
                message3.Arguments.SetString(0, ee.ToString());
                //var success2 = Browser.SendProcessMessage(CefProcessId.Browser, message3);

                return(true);
            }
        }