dll - Why does my code throw an Invalid Cast Exception? (C#)? -


error info: system.invalidcastexception: unable cast object of type 'classlibrary1.plugin' type 'plugininterface.iplugin'.

what i'm trying program access assembly , run whatever may have. loads .dll

private void addplugin(string filename) { assembly pluginassembly = assembly.loadfrom(filename); foreach (type plugintype in pluginassembly.gettypes()) { if (plugintype.ispublic) { if (!plugintype.isabstract) { type typeinterface = plugintype.getinterface("plugininterface… true); if (typeinterface != null) { types.availableplugin newplugin = new types.availableplugin(); newplugin.assemblypath = filename; newplugin.instance = (iplugin)activator.createinstance(plugin… // above line throws exception.  newplugin.instance.initialize(); this.colavailableplugins.add(newplugin); newplugin = null; } typeinterface = null; } } } pluginassembly = null; } 

both program , assembly have these 2 interfaces:

using system;  namespace plugininterface { public interface iplugin { ipluginhost host { get; set; } string name { get; } string description { get; } string author { get; } string version { get; } system.windows.forms.form maininterface { get; } void initialize(); void dispose(); void receivedmessage(playerioclient.message m); void disconnected(); }  public interface ipluginhost { void say(string message); void send(playerioclient.message m); void send(string message_type, params object[] paramss); } } 

my class/assembly add:

using system; using system.collections.generic; using system.linq; using system.text; using system.windows.forms; using system.threading; using plugininterface;  namespace classlibrary1 {  public class plugin : iplugin // <-- see how inherited iplugin interface? { public plugin() {  }  string myname = "title"; string mydescription = "descrip"; string myauthor = "me"; string myversion = "0.9.5";   ipluginhost myhost = null; form1 mymaininterface = new form1();    public string description { { return mydescription; } }  public string author { { return myauthor; } }  public ipluginhost host {  { return myhost; } set { myhost = value; } }  public string name { { return myname; } }  public system.windows.forms.form maininterface { { return mymaininterface; } }  public string version { { return myversion; } }  public void initialize() { //this first function called host... //put needed start here first maininterface.show(); }  public void receivedmessage(playerioclient.message m) {  } public void disconnected() {  }  public void dispose() { maininterface.dispose(); }  } } 

all appreciated.

both program , assembly have these 2 interfaces:

there's problem.

two identical interfaces in 2 different assemblies create 2 distinct (and unrelated) types.

you need define interfaces in single assembly , add reference it.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -