27 lines
813 B
C#
27 lines
813 B
C#
using System.Reflection;
|
|
using System.Runtime.Loader;
|
|
|
|
namespace Apimanager_backend.Data
|
|
{
|
|
// 隔离加载上下文
|
|
public class PluginLoadContext : AssemblyLoadContext
|
|
{
|
|
private readonly AssemblyDependencyResolver _resolver;
|
|
|
|
public PluginLoadContext(string pluginPath) : base(isCollectible: true)
|
|
{
|
|
_resolver = new AssemblyDependencyResolver(pluginPath);
|
|
}
|
|
|
|
protected override Assembly Load(AssemblyName assemblyName)
|
|
{
|
|
// 避免重复加载核心库
|
|
if (assemblyName.Name == "YourSharedContract")
|
|
return null;
|
|
|
|
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
|
|
return assemblyPath != null ? LoadFromAssemblyPath(assemblyPath) : null;
|
|
}
|
|
}
|
|
}
|