I have a single plugin, that takes value for an input and returns the result. I'm loading it like this:
public void load_modules()
{
string path = Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
string pluginDir = Path.GetDirectoryName(path) + "\\Modules";
foreach (string s in Directory.GetFiles(pluginDir, "*.dll"))
{
Type[] pluginTypes = Assembly.LoadFile(s).GetTypes();
foreach (Type t in pluginTypes)
{
if (t.ToString().Contains("Main"))
{
MY_API module = Activator.CreateInstance(t) as MY_API;
GLOBAL_VARIABLES.MODULES.Add(module);
break;
}
}
}
}
Here is the code I'm using to execute plugin:
List<MY_API> plugin_instances = new List<MY_API>(); // define container of the instances
for(int i=0; i<10; i++){
plugin_instances.add(GLOBAL_VARIABLES.MODULES[0]); // loaded module from load_modules()
MessageBox.show(plugin_instances.exec(i)); // execute plugin
}
And I have a timer to monitor to minor status of the first loaded module.
label1.text = GLOBAL_VARIABLES.MODULES[0].getStatus();
The getStatus() function displays what variable is passed to plugin's exec() function.
The problem is I didn't execute the *GLOBAL_VARIABLES.MODULES[0]*, I only used it to make copies off of it. I executed all plugins in *plugin_instances* list, which should contain 10 copies of *GLOBAL_VARIABLES.MODULES[0]*.
To my surprise GLOBAL_VARIABLES.MODULES[0] gets executed, even though it shouldn't, I can see that on label1. Instead of executing 10 instances of my plugin, the GLOBAL_VARIABLES.MODULES[0] gets executed 10 times.
Seems like plugin_instances.exec(i) acts like a reference to GLOBAL_VARIABLES.MODULES[0], instead of individual copy of GLOBAL_VARIABLES.MODULES[0].
How can I get each instance individual, not as reference? Thanks!
Update to reflect question modification:
You need to create a new instance of the module type for each one that you want to execute. You may need to clean the following code up, but it gives you the idea:
plugin_instances.add(Activator.CreateInstance(GLOBAL_VARIABLES.MODULES[0].GetType()));