Ejemplo n.º 1
0
        /// <summary>
        /// Unload module
        /// </summary>
        /// <param name="module">Module</param>
        /// <returns>Task</returns>
        public Task UnloadModuleAsync(IModule module)
        {
            Task ret = new Task(() =>
            {
                if (Modules.UnloadModuleAsync(module).GetAwaiter().GetResult())
                {
                    module.ExitAsync().GetAwaiter().GetResult();
                }
            });

            ret.Start();
            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load module (asynchronous)
        /// </summary>
        /// <param name="path">Path</param>
        /// <returns>Loaded module task</returns>
        public Task <IModule> LoadModuleAsync(string path)
        {
            Task <IModule> ret = new Task <IModule>(() => {
                IModule result = null;
                try
                {
                    if (path != null)
                    {
                        if (File.Exists(path))
                        {
                            string full_path  = System.IO.Path.GetFullPath(path);
                            Assembly assembly = Assembly.LoadFile(full_path);
                            if (assembly != null)
                            {
                                Type[] types = assembly.GetTypes();
                                if (types != null)
                                {
                                    bool has_module_type = false;
                                    foreach (Type type in types)
                                    {
                                        if (type != null)
                                        {
                                            if (type.IsClass)
                                            {
                                                if (typeof(IModule).IsAssignableFrom(type))
                                                {
                                                    if (has_module_type)
                                                    {
                                                        has_module_type = false;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        has_module_type = true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (has_module_type)
                                    {
                                        foreach (Type type in types)
                                        {
                                            if (type != null)
                                            {
                                                if (type.IsClass)
                                                {
                                                    object instance = null;
                                                    if (Attribute.IsDefined(type, typeof(ServiceAttribute)))
                                                    {
                                                        instance = serviceProvider.RequireService(type);
                                                    }
                                                    if (typeof(IModule).IsAssignableFrom(type))
                                                    {
                                                        instance = ((instance == null) ? Activator.CreateInstance(type, true) : instance);
                                                        result   = (IModule)instance;
                                                        if (loadedModules.ContainsKey(result.Name))
                                                        {
                                                            IModule old_module = loadedModules[result.Name];
                                                            old_module.ExitAsync().GetAwaiter().GetResult();
                                                            loadedModules[result.Name] = result;
                                                        }
                                                        else
                                                        {
                                                            loadedModules.Add(result.Name, result);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        if (result != null)
                                        {
                                            foreach (IModule loaded_module in loadedModules.Values)
                                            {
                                                loaded_module.ModuleUnloadAsync(result).GetAwaiter().GetResult();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);
                }
                return(result);
            });

            ret.Start();
            return(ret);
        }