using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.Loader; using System.Text; using System.Threading.Tasks; namespace Serein.NodeFlow.Tool { /// /// 管理加载在流程的程序集 /// public class FlowLibraryLoader : AssemblyLoadContext { private Assembly _pluginAssembly; /// /// 加载程序集 /// /// public FlowLibraryLoader(string pluginPath) : base(isCollectible: true) { _pluginAssembly = LoadFromAssemblyPath(pluginPath); } /// /// 保持默认加载行为 /// /// /// protected override Assembly Load(AssemblyName assemblyName) { return null; // 保持默认加载行为 } /// /// 是否对程序集的引用 /// public void UnloadPlugin() { _pluginAssembly = null; // 释放对程序集的引用 Unload(); // 触发卸载 // 强制进行垃圾回收,以便完成卸载 GC.Collect(); GC.WaitForPendingFinalizers(); } } }