Isolation of static member variables in AssemblyLoadContext #69899
-
In my framework I currently have a static logger class But this seems not to be the case as the static members of my logger class seem to be shared. Imagine I have a Logger class public static class Logger
{
private static readonly List<string> _messages = new();
private static void Log(string message)
{
_messages.Add(message);
}
} and a plugin (in Plugin.dll) public class Plugin
{
private void Run()
{
Logger.Log("Plugin")
}
} and the plugin is create like this public static void Main()
{
Logger.Log("Main");
AssemblyLoadContext ctx = new("PluginLoader", true);
var assembly = ctx.LoadFromAssemblyPath("Plugin.dll");
Plugin plugin = assembly.CreateInstance("Plugin", false, BindingFlags.Default, null, null, null, null) as Plugin;
plugin.Run();
} Logger does now contain two entries (Main and Plugin). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You need to explicitly load the logging framework into the |
Beta Was this translation helpful? Give feedback.
You need to explicitly load the logging framework into the
PluginLoader
context, otherwise it uses the already-loaded assembly from the main context.