/// <summary> /// 初始化插件。 /// </summary> /// <param name="pluginDescriptor">插件描述</param> private static void Initialize(PluginDescriptor pluginDescriptor) { //使用插件名称做为字典 KEY。 string key = pluginDescriptor.Plugin.Name; //不存在时才进行初始化。 if (!_plugins.ContainsKey(key)) { //初始化。 pluginDescriptor.Plugin.Initialize(); //增加到字典。 _plugins.Add(key, pluginDescriptor); } }
/// <summary> /// 加载插件。 /// </summary> public static IEnumerable<PluginDescriptor> Load() { List<PluginDescriptor> plugins = new List<PluginDescriptor>(); ////程序集复制到临时目录。 //FileCopyTo(); //IEnumerable<Assembly> assemblies = null; //////加载 bin 目录下的所有程序集。 ////assemblies = AppDomain.CurrentDomain.GetAssemblies(); ////plugins.AddRange(GetAssemblies(assemblies)); ////加载临时目录下的所有程序集。 //assemblies = TempPluginFolder.GetFiles("*.dll", SearchOption.AllDirectories).Select(x => Assembly.LoadFile(x.FullName)); ////assemblies = TempPluginFolder.GetFiles("Weikeren.GroupBuy.Sell.dll", SearchOption.AllDirectories).Select(x => Assembly.LoadFile(x.FullName)); //plugins.AddRange(GetAssemblies(assemblies)); //return plugins; try { string pluginAssemblyMappingPath = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "PluginAssemblyMapping.xml"); var pluginList = XElement.Load(pluginAssemblyMappingPath); var mappings = from m in pluginList.Elements() select new PluginAssemblyMapping() { PluginPath = m.Element("PluginPath").Value, AssemblyName = m.Element("AssemblyName").Value, InimplementIPlugClassName = m.Element("ImplementIPlugClassName").Value }; var list = mappings.ToList(); if (list != null && list.Count > 0) { //var context = HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) as HttpContextBase : (new FakeHttpContext("~/") as HttpContextBase); foreach (var item in list) { //string path = context.Current.Server.MapPath(string.Format("{0}/{1}", item.PluginPath.Trim('/'), item.AssemblyName)); string path = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, string.Format("{0}\\{1}", item.PluginPath.TrimStart('~').Trim('/').Trim('\\'), item.AssemblyName)); //var assembly = Assembly.LoadFile(path); //此方法不能释放dll,因为dll不能被覆盖 var assembly = loadAssembly(path); var plugin = (IPlugin)assembly.CreateInstance(item.InimplementIPlugClassName); if (plugin != null) { var desc = new PluginDescriptor(plugin, assembly, assembly.GetTypes()); plugins.Add(desc); } } } return plugins; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("加载PluginAssemblyMapping.xml失败。"); return plugins; } }
/// <summary> /// 卸载。 /// </summary> public static void Unload(PluginDescriptor pluginDescriptor) { pluginDescriptor.Plugin.Unload(); _plugins.Remove(pluginDescriptor.Plugin.ToString()); }