C# - Error casting object to interface -


yes, there similar questions, none of them solved problem. created new solution 3 projects:

  • firstplugin: library project, compiled dll.
  • mainapp: console application, import firstplugin.
  • shared: shared project interface declared. both firstplugin , mainapp projects have project on it's references.

shared project

the project structure:

shared project structure

the icrawler.cs code:

namespace shared.data.structures {     public interface icrawler     {         void sayhello();     } } 

firstplugin project

the project structure:

firstplugin structure

the fp.cs code:

using system; using shared.data.structures;  namespace firstplugin {     public class fp : icrawler     {         public void sayhello() {             console.writeline("hello firstplugin.dll");         }     } } 

mainapp project

the project structure:

mainapp project structure

the program.cs code:

using system; using system.collections.generic; using system.reflection; using system.io; using shared.data.structures;  namespace mainapp {     class program     {         static void main(string[] args) {             icollection<icrawler> plugins = genericpluginloader<icrawler>.loadplugins(directory.getcurrentdirectory() + "\\modules");             console.readkey();         }     }      public static class genericpluginloader<t>     {         public static icollection<t> loadplugins(string path) {             string[] dllfilenames = null;              if (directory.exists(path)) {                 dllfilenames = directory.getfiles(path, "*.dll");                  icollection<assembly> assemblies = new list<assembly>(dllfilenames.length);                 foreach (string dllfile in dllfilenames) {                     assemblyname = assemblyname.getassemblyname(dllfile);                     assembly assembly = assembly.load(an);                      assemblies.add(assembly);                 }                  type plugintype = typeof(t);                 icollection<type> plugintypes = new list<type>();                 foreach (assembly assembly in assemblies) {                     if (assembly != null) {                         type[] types = assembly.gettypes();                          foreach (type type in types) {                             if (type.isinterface || type.isabstract) {                                 continue;                             }                             else {                                 if (type.getinterface(plugintype.fullname) != null) {                                      console.writeline("icrawler name: {0}", typeof(icrawler).assemblyqualifiedname);                                     console.writeline("type name: {0}", type.getinterface(plugintype.fullname).assemblyqualifiedname);                                      /*                                         output:                                          icrawler name: shared.data.structures.icrawler, mainapp, version=1.0.0.0, culture=neutral, publickeytoken=null                                         type name: shared.data.structures.icrawler, firstplugin, version=1.0.0.0, culture=neutral, publickeytoken=null                                     */                                      plugintypes.add(type);                                 }                             }                         }                     }                 }                  icollection<t> plugins = new list<t>(plugintypes.count);                 foreach (type type in plugintypes) {                     t plugin = (t)activator.createinstance(type);                     plugins.add(plugin);                 }                  return plugins;             }              return null;         }     } } 

the error

i'm getting nice , beautiful error:

additional information: unable cast object of type 'firstplugin.fp' type 'shared.data.structures.icrawler'

on line (program.cs):

t plugin = (t)activator.createinstance(type); 

i decided create solution , copy paste exact genericpluginloader source (from msdn).

the project i'm working on has different code, same error occur.

what's wrong code?

my build output:

  • d:\plugintest\modules\firstplugin.dll
  • d:\plugintest\mainapp.exe

ps: english not native language, so... know (╭☞ ͠° ͟ʖ °)╭☞.

shared projects compile source files directly each project references them.

therefore, have 2 icrawler interfaces, 1 in each assembly, not same type (even though they're identical).

you're trying cast new instance copy of interface doesn't implement; can't that.

you should use normal class library, not shared project.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -