vendredi 1 avril 2016

The SelectedIndex of a combobox is always -1 when using reflection

I have a plugin system for my application that is loading plugins via reflection. When a plugin is loaded the plugins controls and the associated code is copied to a tab control. For a test plugin I have two controls: A combo box and a button. When the button is clicked a message box displays and should show the "Selected Index" of the combo box. Instead, no matter what item I select on the combo box, the "Selected Index" is always returned as -1.

I am using this line of code for the button:

 MessageBox.Show("The Selected Index Is: " + comboBox1.SelectedIndex.ToString());

I am using the following to load the plugin (the variable filename is the plugin's file location as a string):

 Assembly assembly = Assembly.LoadFrom(filename);
 Type[] types = assembly.GetTypes();
 Boolean ContainsIPlugin = false;
 String pluginname = filename.Substring(filename.LastIndexOf("\\") + 1);
 pluginname = pluginname.Substring(0, pluginname.LastIndexOf("."));
 // look for our interface in the assembly
 foreach (Type type in types)
 {
     // if we found our interface, verify attributes
     if (type.GetInterface("IPlugin") != null)
     {
         ContainsIPlugin = true;

         // create the plugin using reflection
         IPlugin pluginobj = Activator.CreateInstance(type) as IPlugin;

         // get custom attributes from plugin
         Plugin newplugin = new Plugin(pluginobj);
         newplugin.type = type;
         newplugin.name = pluginobj.PluginDisplayName;
         newplugin.version = pluginobj.PluginVersion;
         newplugin.description = pluginobj.PluginDescription;
         newplugin.author = pluginobj.PluginAuthor;
         newplugin.filename = filename;

         TabPage newpage = new TabPage();
         newpage.Name = newplugin.name;
         newpage.Text = newplugin.name;
         newpage.Controls.AddRange(newplugin.controls.ToArray<Control>());
         tabctrMENU.TabPages.Add(newpage);
         lstMENU.Items.Add("     " + newplugin.name);
     }
 }

 if (!ContainsIPlugin)
 {
     throw new InvalidOperationException(pluginname + " Plugin must implement IPlugin interface!");
 }

I am planning on loading the plugins in a separate app domain so I can delete the plugin from the application without getting a "file is in use" exception. I am still researching how to implement that and I don't know if that will fix either issue. Any suggestions to fix both issues would be greatly appreciated.





Aucun commentaire:

Enregistrer un commentaire