whoever reading this if you are having the same issue take a look at this github issue https://github.com/dotnet/runtime/issues/119648. In the github issue you will find the solution but for reference here is the code that works:
using System;
using System.Reflection;
using System.Runtime.Loader;
namespace Host {
public class PluginAssemblyLoadContext : AssemblyLoadContext {
public PluginAssemblyLoadContext() : base(true) { }
}
public class HostState {
public Assembly pluginAssembly;
public PluginAssemblyLoadContext loadContext;
public Action pluginExecute;
}
public static class Host {
public const string PLUGIN_PATH = "../plugin/bin/Debug/net10.0/plugin.dll";
public static void Main() {
HostState host = new HostState();
host.loadContext = new PluginAssemblyLoadContext();
ReloadDll(host);
Console.WriteLine("Press E to execute the plugin, R to reload, or Q to quit.");
for (;;) {
var key = Console.ReadKey(true);
if (false) {
} else if (key.Key == ConsoleKey.E) {
host.pluginExecute.Invoke();
} else if (key.Key == ConsoleKey.R) {
ReloadDll(host);
} else if (key.Key == ConsoleKey.Q) {
return;
}
}
}
public static void ReloadDll(HostState host) {
host.loadContext = new PluginAssemblyLoadContext();
byte[] assemblyBytes = File.ReadAllBytes(Path.GetFullPath(PLUGIN_PATH));
using MemoryStream ms = new MemoryStream(assemblyBytes);
host.pluginAssembly = host.loadContext.LoadFromStream(ms);
Type type = host.pluginAssembly.GetType("Plugin");
MethodInfo methodInfo = type.GetMethod("Execute", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, []);
if (methodInfo != null) {
host.pluginExecute = methodInfo.CreateDelegate<Action>();
}
}
}
}