public MapResult Handle()
        {
            var       mapInfo = this._context.MapInfo;
            var       browser = this._context.WebBrowserInfo.WebBrowser;
            MapResult result  = null;
            //Type t = Type.GetType(mapInfo.Path);
            Type t = this._context.GetTypeByString(mapInfo.Path);

            object[] args = new object[mapInfo.Args.Count];
            for (int i = 0; i < args.Length; i++)
            {
                var(type, value) = this._context.JsValueInfoToNative(mapInfo.Args[i]);
                if (value is Tuple <long, object> )
                {
                    var(nid, obj) = value as Tuple <long, object>;
                    args[i]       = obj;
                }
                else
                {
                    args[i] = value;
                }
            }

            var o           = Activator.CreateInstance(t, args);
            var genericInfo = this._context.GetGenericInfo(t);
            var id          = this._context.WebBrowserInfo.AddNativeObject(o, genericInfo);

            result = new MapResult {
                Status = true, Data = new MapDataInfo {
                    DataType = MapDataType.NativeObjectId, Value = new { Id = id, GenericInfo = genericInfo }
                }
            };
            return(result);
        }
Exemplo n.º 2
0
        static void OnJavaScriptNativeMap(IWebBrowser browser, JsQueryEventArgs args)
        {
            MapResult result = null;
            var       info   = This.GetBrowserInfo(browser);

            try
            {
                var json    = args.Request;
                var mapInfo = JsonConvert.DeserializeObject <MapActionInfo>(json);
                var context = new JsNativeInvokeContext(info, mapInfo, (typeName) => NativeTypeDic.ContainsKey(typeName) ? NativeTypeDic[typeName] : null);
                result = JsNativeInvokeHandleFactory.This.CreateHandle(context).Handle();
                if (result == null)
                {
                    browser.ResponseJsQuery(args.WebView, args.QueryId, args.CustomMsg, "OK");
                    return;
                }
                var resultJson = JsonConvert.SerializeObject(result);
                browser.ResponseJsQuery(args.WebView, args.QueryId, args.CustomMsg, resultJson);
            }
            catch (Exception ex)
            {
                if (result != null && result.Data.DataType == MapDataType.NativeObjectId)
                {
                    info.DestroyNativeObject(((NativeObjectInfo)result.Data.Value).Id, false);
                }
                result = new MapResult {
                    Status = false, Data = new MapDataInfo {
                        DataType = MapDataType.Value, Value = ex.Message
                    }
                };
                var resultJson = JsonConvert.SerializeObject(result).Replace("\\r", "").Replace("\\n", "");
                browser.ResponseJsQuery(args.WebView, args.QueryId, args.CustomMsg, resultJson);
            }
        }
        public MapResult Handle()
        {
            var       mapInfo = this.Context.MapInfo;
            var       browser = this.Context.WebBrowserInfo.WebBrowser;
            MapResult result  = null;

            object[] args  = new object[mapInfo.Args.Count];
            Type[]   types = new Type[mapInfo.Args.Count];
            for (int i = 0; i < mapInfo.Args.Count; i++)
            {
                var(type, value) = this.Context.JsValueInfoToNative(mapInfo.Args[i]);
                if (value is Tuple <long, object> )
                {
                    var(id, obj) = value as Tuple <long, object>;
                    args[i]      = obj;
                }
                else
                {
                    args[i] = value;
                }
                types[i] = type;
            }
            object r = InvokeMethod(types, args);

            if (r == null)
            {
                result = new MapResult {
                    Status = true, Data = new MapDataInfo {
                        DataType = MapDataType.Value, Value = null
                    }
                };
            }
            else
            {
                var nativeTypeName = this.Context.GetJsTypeName(r.GetType().FullName);
                if (r.GetType().IsValueType || nativeTypeName == null)
                {
                    result = new MapResult {
                        Status = true, Data = new MapDataInfo {
                            DataType = MapDataType.Value, Value = r
                        }
                    };
                }
                else
                {
                    var id          = this.Context.WebBrowserInfo.GetNativeObjectId(r);
                    var genericInfo = "";
                    if (id == -1)
                    {
                        genericInfo = this.Context.GetGenericInfo(r.GetType());
                        id          = this.Context.WebBrowserInfo.AddNativeObject(r, genericInfo);
                    }
                    result = new MapResult {
                        Status = true, Data = new MapDataInfo {
                            DataType = MapDataType.NativeObjectId, Value = new NativeObjectInfo {
                                Id = id, GenericInfo = genericInfo, Path = r.GetType().FullName
                            }
                        }
                    };
                }
            }
            return(result);
        }