c# - How can I use parts of code with a "3rd party" doing the implementation of an interface? -


to add information. 3rd party implementations adhere interface.

given implementation of interface "hidden" me. how can use hidden class implementation own program?

this interface structure looks like.

namespace companyname.integration.class  public interface classinterface{     //some methods } 

and in program i'm writing implement stuff:

namespace companyname.integration.class  namespace myprogram{     class program{         static void main(string[] args){             //here want use not yet implemented/"hidden" implementation of interface           }     } } 

how can use class isn't implemented , know/have access interface of?

because cannot new class = new interface. how solve problem?

can extend interface or give class interface kind of list<e>?

you scan assembly types implement interface via reflection:

var types = assembly.getassembly(typeof(myinterface))     .gettypes()     .where(x => x.isassignablefrom(x) && type.getconstructor(type.emptytypes) != null); 

now can instantiate classes. aware types may contain multiple types implement interface. simplicity chose first:

var newinstance = activator.createinstance(types.first()); 

however noted within comments it´s possible vendor of assembly didn´t provide class implement interface, or such type exists in assembly. furthermore it´s possible class(es) don´t have default-constructor.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -