Created
May 6, 2025 19:59
-
-
Save jpd21122012/3d77914a5d7f64a271d58bfb5dee710e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.Loader; | |
public class PluginLoadContext : AssemblyLoadContext | |
{ | |
public PluginLoadContext() : base(isCollectible: true) { } | |
protected override Assembly? Load(AssemblyName assemblyName) | |
{ | |
// Avoid loading dependencies into this context (prevents conflicts) | |
return null; | |
} | |
} | |
public class PluginReloader | |
{ | |
private readonly Dictionary<string, (PluginLoadContext Context, Assembly Assembly)> _loadedPlugins = new(); | |
public void LoadPlugin(string pluginPath) | |
{ | |
var context = new PluginLoadContext(); | |
var assembly = context.LoadFromAssemblyPath(pluginPath); | |
_loadedPlugins[pluginPath] = (context, assembly); | |
} | |
public void UnloadPlugin(string pluginPath) | |
{ | |
if (_loadedPlugins.TryGetValue(pluginPath, out var plugin)) | |
{ | |
plugin.Context.Unload(); | |
_loadedPlugins.Remove(pluginPath); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment