示例#1
0
        public static bool AddByAssembly(params string[] SocketPath)
        {
            for (int i = 0; i < SocketPath.Length; i++)
            {
                try
                {
                    if (SocketPath != null && SocketPath.Length > 0)
                    {
                        string path = SocketPath[i];
                        if (!File.Exists(path))
                        {
                            string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            path = StringUtils.PathMaker(dir, path);
                            if (!File.Exists(path))
                            {
                                continue;
                            }
                        }
                        WebSocketAdding?.Invoke(string.Format(Language["STR_LOG_WEB_SOCKET_LOADING"], SocketPath[i]), null);

                        Assembly asm = Assembly.LoadFrom(path);

                        /*
                         * IEnumerable<string> result = asm.GetTypes()
                         *  .Where(type =>
                         *  type.Namespace == NameSpace)
                         *  .Select(type => type.Name);
                         */

                        foreach (Type type in asm.GetTypes())
                        {
                            try
                            {
                                Attribute[] attrs = Attribute.GetCustomAttributes(type);
                                foreach (Attribute attr in attrs)
                                {
                                    if (attr is RouterAttribute module)
                                    {
                                        try { Add(module.Path, (WebSocketProc)Activator.CreateInstance(type)); }
                                        catch (Exception ex) { WebSocketAdding?.Invoke(string.Format(Language["STR_LOG_WEB_SOCKET_ERROR"], type.Name), ex); }
                                    }
                                }
                            }
                            catch (Exception ex) { WebSocketAdding?.Invoke(Language["STR_LOG_WEB_SOCKET_ERROR"], ex); return(false); }
                        }
                    }
                }
                catch (Exception ex) { WebSocketAdding?.Invoke(Language["STR_LOG_WEB_SOCKET_ERROR"], ex); return(false); }
            }

            return(true);
        }
示例#2
0
        public static bool Add(string WebPath, WebSocketProc Module)
        {
            string Name = Module.GetType().Name;

            if (!Modules.ContainsKey(WebPath))
            {
                Modules.Add(WebPath, Module);
                WebSocketAdding?.Invoke(string.Format("[Loaded] WebSocket: [{0}] {1}", WebPath, Name), null);
                return(true);
            }
            else
            {
                WebSocketAdding?.Invoke(string.Format("[Exist!] WebSocket: [{0}] {1}", WebPath, Name), null);
                return(false);
            }

            //if (Modules.ContainsKey(Path)) Modules[Path] = Module;
            //else Modules.Add(Path, Module);
        }