i have generic static method registers interface , need write using c# reflection.
services.addservice<itbroker>(new tbrokerservice());
i tried following code not working
type[] externtbrokerservice = assembly.loadfrom("business.dll").gettypes(); type[] externservice = assembly.loadfrom("servicemodel.dll").gettypes(); type itbroker = externitbroker[12]; methodinfo method = externservice[1].getmethods()[2]; //gets add service method methodinfo generic = method.makegenericmethod(itbroker); //make method generic generic.invoke(null,new object[] { externtbrokerservice[0]}); //invoke service
above code gives me generic exception of parameters.
what write way write reflection above code?
as in comments:
note externtbrokerservice[0]
type
, not instance of type.
having feel need include sense of other comments part of answer.
type itbroker = externitbroker[12];
this wrong! , sooner or later fail find type order of types in collection undetermined , can change. should this:
type itbroker = externitbroker.single(x => x.name == "itbroker");
this far form foolproof sure condition gives unique result.
or load type directly (assuming assemblyqualifiedname of type):
type.gettype("business.itbroker, business");
to find method on type there method type.getmethod
1 of overloads sufficient find method.
to create instance of type needs passed argument can use
activator.createinstance(brokerservicetype);
Comments
Post a Comment