-2

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!

user1015551
  • 197
  • 3
  • 20
  • where are you making the copy of `GLOBAL_VARIABLES.MODULES[0]` ? All your current code is showing is that you add the same instance to the list 10 times, so of course that instance is executed 10 times. – BrokenGlass Nov 07 '11 at 01:27

2 Answers2

0

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()));
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • There is only one module loaded from the directory, therefor I use [0] instead of [i]. GLOBAL_VARIABLES.MODULES contains only one module from which I need to make 10 copies – user1015551 Nov 07 '11 at 01:54
  • I understand now: you need to create a new instance of the plugin module on each pass through the loop, not just reuse the existing one. I have updated the answer to reflect this. – competent_tech Nov 07 '11 at 02:00
0

Since you want to make a copy and the type you want to make a copy of has a parameter-less constructor you could just dynamically create a new instance:

 var moduleType = GLOBAL_VARIABLES.MODULES[0].GetType();
 for(int i=0; i<10; i++)
 {
     plugin_instances.add((MY_API)Activator.CreateInstance(moduleType)); 
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335