I am experimenting with a plugin system. So that DLL files are read from a folder and these functions are then added to the main application.
All DLL files contain the interface IPlugin (project: Plugin):
public interface IPlugin
{
void SetValue();
string GetValue();
}
The Plugins are in seperate projects:
public class PluginAB : IPlugin
{
public string GetValue()
{
return SomeStaticValues.StaticValues.Value;
}
public void SetValue()
{
SomeStaticValues.StaticValues.Value = "AB";
}
}
There are two "plugins" AB and XY (which differ only in writing "XY" instead of "AB".
All they do is write a static value in another project:
public static class StaticValues
{
public static string Value { get; set; }
}
The references looks like the following drawing:
The IPlugin objects from the DLL's are created with InvokeMember():
private IPlugin LoadPlugin(string iPath)
{
Assembly lAssambly = Assembly.LoadFrom(iPath);
foreach (Type lType in lAssambly.GetTypes())
{
if (lType.GetInterfaces().Contains(typeof(IPlugin)))
{
return lType.InvokeMember(null, BindingFlags.CreateInstance, null, null, null) as IPlugin;
}
}
return null;
}
As you can see in the reference picture, the main app also has access to the "StaticValues" class.
If I call the methods in the main app like this:
static void Main(string[] args)
{
IPlugin lPluginAB = LoadPlugin("AB");
IPlugin lPluginXY = LoadPlugin("XY");
lPluginAB.SetValue();
Console.WriteLine(lPluginAB.GetValue());
lPluginXY.SetValue();
Console.WriteLine(lPluginAB.GetValue());
SomeStaticValues.StaticValues.Value = "Test";
Console.WriteLine(lPluginAB.GetValue());
Console.ReadLine();
}
I would expect:
AB XY Test
instead I am getting:
AB XY XY
if I check directly
Console.WriteLine(SomeStaticValues.StaticValues.Value);
I am getting
Test
Why is a static value different if I access it from a loaded dll file instead of the main application?