Skip to content

Instantly share code, notes, and snippets.

@jpd21122012
Created May 6, 2025 19:59
Show Gist options
  • Save jpd21122012/3d77914a5d7f64a271d58bfb5dee710e to your computer and use it in GitHub Desktop.
Save jpd21122012/3d77914a5d7f64a271d58bfb5dee710e to your computer and use it in GitHub Desktop.
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