Files
serein-flow/NodeFlow/Tool/FlowLibraryLoader.cs

54 lines
1.3 KiB
C#
Raw Normal View History

2024-11-02 16:48:40 +08:00
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
{
/// <summary>
/// 管理加载在流程的程序集
/// </summary>
public class FlowLibraryLoader : AssemblyLoadContext
{
private Assembly _pluginAssembly;
/// <summary>
/// 加载程序集
/// </summary>
/// <param name="pluginPath"></param>
public FlowLibraryLoader(string pluginPath) : base(isCollectible: true)
{
_pluginAssembly = LoadFromAssemblyPath(pluginPath);
}
/// <summary>
/// 保持默认加载行为
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
protected override Assembly Load(AssemblyName assemblyName)
{
return null; // 保持默认加载行为
}
/// <summary>
/// 是否对程序集的引用
/// </summary>
public void UnloadPlugin()
{
_pluginAssembly = null; // 释放对程序集的引用
Unload(); // 触发卸载
// 强制进行垃圾回收,以便完成卸载
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}