private Assembly PluginManagerOnAssemblyResolve(object sender, ResolveEventArgs args) { try { // AssemblyName name = new AssemblyName(args.Name); AssemblyNameReference name = AssemblyNameReference.Parse(args.Name); if (IsLoaded(name, out Assembly loadedPluginAssembly)) { lock (_pluginLock) { if (!AssemblyReferences.ContainsKey(name.Name)) { AssemblyReferences.TryAdd(name.Name, loadedPluginAssembly); } } return(loadedPluginAssembly); } string rootPath = ""; if (args.RequestingAssembly != null && !string.IsNullOrWhiteSpace(args.RequestingAssembly.Location)) { rootPath = Path.GetDirectoryName(args.RequestingAssembly.Location); } Assembly result = null; if (TryFindAssemblyPath(name, rootPath, out string resultPath)) { result = Assembly.LoadFile(resultPath); } else { var assembly = AppDomain.CurrentDomain.GetAssemblies() .FirstOrDefault(x => x.GetName().Name == args.Name); if (assembly != null) { result = assembly; } } if (result != null) { AssemblyReferences.TryAdd(name.Name, result); } else { Log.Warn($"Failed to resolve assembly: {name}"); } return(result); } catch (Exception ex) { Log.Error($"Failed to resolve!", ex); return(null); } }
/// <summary> /// 移除程序集对应的引用 /// 优先:移除动态编译的引用 /// 其次:移除插件加载方式的引用 /// </summary> /// <param name="assembly"></param> public override void Remove(Assembly assembly) { if (assembly != null) { if (AssemblyReferences.ContainsKey(assembly)) { while (!AssemblyReferences.TryRemove(assembly, out _)) { } } else if (assembly.Location != "" && assembly.Location != default) { Remove(assembly.Location); } } }