/// <summary> /// 加载插件 /// </summary> /// <param name="path"></param> private void loadCSharpPlugin(string path) { string name = Path.GetFileName(path); if (!File.Exists(path)) { Log.Write("加载插件", name, "失败, 原因 --> ", "插件路劲下不存在该插件!"); return; } try { Assembly assembly = Assembly.LoadFrom(path); Plugin plugin = new Plugin(); plugin.PluginInstance = Activator.CreateInstance( assembly.GetExportedTypes().Where(p => p.GetCustomAttribute <ExportAttribute>() != null).First() ) as IPlugBaseInterface; if (plugin is null) { Log.Write("加载插件", name, "失败, 原因 --> ", "插件未提供有效的接口!"); return; } PlugInfo plugInfo = new PlugInfo(); FileInfo info = new FileInfo(path); plugin.PlugInfo = plugInfo; plugInfo.Name = plugin.PluginInstance.Name; plugInfo.LocalURL = path; plugInfo.RemoteURL = ""; plugInfo.IsInstall = true; plugInfo.Size = info.Length.ToString(); plugInfo.Version = assembly.ImageRuntimeVersion; plugInfo.DateTime = info.LastWriteTime; plugInfo.Author = ""; string id = EncryptWithMD5(name); if (!PluginsContainer.ContainsKey(id)) { PluginsContainer.Add(id, plugin); } else { Log.Write("加载插件", name, "失败, 原因 --> 插件已经加载,重复加载!"); return; } } catch (Exception ex) { Log.Write("加载插件", name, "失败, 原因 --> ", ex.Message, "或者导出插件接口没有添加特性ExportAttribute"); } }
/// <summary> /// 获取本地指定路径下的插件 /// </summary> /// <param name="path"></param> /// <returns></returns> private List <IPlugInfoInterface> getPlugInfoInterfaces(string path) { List <IPlugInfoInterface> plugs = new List <IPlugInfoInterface>(); string[] directorys = Directory.GetDirectories(path); foreach (var directorysitem in directorys) { string[] fileNames = Directory.GetFiles(directorysitem); foreach (var item in fileNames) { try { if (Assembly.LoadFrom(item).GetExportedTypes().Where(p => p.GetCustomAttribute <ExportAttribute>() != null).First() == null) { continue; } } catch (Exception ex) { continue; } FileInfo info = new FileInfo(item); PlugInfo plugInfo = new PlugInfo(); plugInfo.DateTime = info.LastWriteTime; plugInfo.IsInstall = true; plugInfo.LocalURL = item; plugInfo.Name = Assembly.LoadFrom(item).GetName().Name; plugInfo.Size = info.Length.ToString(); plugInfo.Version = Assembly.LoadFrom(item).ImageRuntimeVersion; plugInfo.Author = ""; info = null; plugInfo.Id = Guid.NewGuid().ToString() + DateTime.Now.ToString(); plugs.Add(plugInfo); } } return(plugs); }