private bool CreatePlugInConfiguration() { if (!File.Exists(_configFileName)) { _log.Warn("文件[" + _configFileName + "]不存在!"); //不存在,需要自动创建XML文档,所有信息由自定制的特性得到, XmlTextWriter emptyXmlDoc = new XmlTextWriter(_configFileName, Encoding.Default); emptyXmlDoc.WriteStartDocument(); emptyXmlDoc.WriteStartElement("config"); emptyXmlDoc.WriteEndElement(); emptyXmlDoc.Flush(); emptyXmlDoc.Close(); } //创建菜单的XML文档 XmlDocument plugInDoc = new XmlDocument(); try { plugInDoc.Load(_configFileName); } catch { _log.Error("载入菜单文档时出错!\n"); throw; } if (_plugInLoader == null) { throw new ArgumentNullException("plugInLoader", "指定的加载域为空"); } //遍历指定路径的满足条件的插件 string[] paths = _assemblySetPath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); List <string> plugInFiles = new List <string>(); foreach (string path in paths) { string[] plgs = Directory.GetFiles(path, "*.dll"); plugInFiles.AddRange(plgs); } foreach (string pluginfile in plugInFiles) { //创建读取插件模块的自制特性 try { //Assembly assembly = Assembly.LoadFrom(plugInFiles[i]); Assembly assembly = _plugInLoader.RemoteLoadAssembly(pluginfile); if (assembly == null) { continue; } AttributesReader attrReader = new AttributesReader(assembly); PluginAttribute[] attrPlugInInfo = attrReader.GetPlugInMenuInfoAttribute(); for (int menuIndex = 0; menuIndex < attrPlugInInfo.Length; menuIndex++) { if (!(attrPlugInInfo[menuIndex] is ClientPluginAttribute)) { continue; } ClientPluginAttribute attrMenuPluginInfo = attrPlugInInfo[menuIndex] as ClientPluginAttribute; //检查所给定的启动类是否实现了_assemblyIntfType接口 Type startupInterfaceType = attrMenuPluginInfo.StartupClassType.GetInterface(_assemblyInterfaceType); if (startupInterfaceType == null) { _log.Warn("所给定的启动类没有实现[" + _assemblyInterfaceType + "]接口!"); break; } XmlNode plugInNode = plugInDoc.DocumentElement.SelectSingleNode( "//plugin[@library='" + Path.GetFileName(pluginfile) + "'" + " and @class='" + attrMenuPluginInfo.StartupClassType.FullName + "']"); if (plugInNode != null) { break; } Hashtable ht = new Hashtable(); ht.Add("caption", attrMenuPluginInfo.MenuNameSubsystem); XmlNode subSystemRootNode = FindAndCreateXmlNode(plugInDoc, plugInDoc.DocumentElement , "system[@caption='" + attrMenuPluginInfo.MenuNameSubsystem + "']" , "system", ht); ht.Clear(); ht.Add("caption", attrMenuPluginInfo.MenuNameParent); XmlNode subSystemPlugInsNode = FindAndCreateXmlNode(plugInDoc, subSystemRootNode , "plugins[@caption='" + attrMenuPluginInfo.MenuNameParent + "']" , "plugins", ht); ht.Clear(); ht.Add("caption", attrMenuPluginInfo.MenuNameAssembly); ht.Add("library", Path.GetFileName(pluginfile)); ht.Add("class", attrMenuPluginInfo.StartupClassType.FullName); if (!String.IsNullOrEmpty(attrMenuPluginInfo.IconName)) { ht.Add("iconname", attrMenuPluginInfo.IconName); } FindAndCreateXmlNode(plugInDoc, subSystemPlugInsNode , "plugin[@library='" + Path.GetFileName(pluginfile) + "'" + " and @class='" + attrMenuPluginInfo.StartupClassType.FullName + "']" , "plugin", ht); } } catch (Exception e) { _log.Error(e.ToString()); continue; } } plugInDoc.Save(_configFileName); _log.Info("Config文件更新完成"); return(true); }
internal static Assembly LoadPlugIn(string appPath, string assemblyName) { EnsurePlugInLoader(appPath); return(_plugInLoader.RemoteLoadAssembly(assemblyName)); }