c# - Get all inherited classes of a generic abstract class -



c# - Get all inherited classes of a generic abstract class -

i'm looking way classes inherit generic abstract class, , perform method on each of classes.

i've been next change parameter type when implementing abstract method have class implementations want, similar this:

class="lang-cs prettyprint-override">public abstract class abstractrequest<tresponsedata> tresponsedata : iresponsedata { public abstract void search(); public abstract gooddata binddata(tresponsedata data); } public interface iresponsedata { } public class aresponse : iresponsedata { } public class bresponse : iresponsedata { } public class : abstractrequest<aresponse> { public override void search() { // aresponse // phone call binddata() aresponse } public override gooddata binddata(aresponse data) { // bind info aresponse gooddata } } public class b : abstractrequest<bresponse> { public override void search() { // bresponse // phone call binddata() bresponse } public override gooddata binddata(bresponse data) { // bind info bresponse gooddata } }

this working find, until need & b class , invoke search() method each of classes. non-generic abstract class, utilize next snippet classes

class="lang-cs prettyprint-override">var instances = t in assembly.getexecutingassembly().gettypes() t.issubclassof(typeof(abstractrequest)) && t.getconstructor(type.emptytypes) != null select activator.createinstance(t) abstractrequest;

then, how can classes , b inherit abstractrequest<aresponse> , abstractrequest<bresponse>?

edit: forgot mention. let's assume there many implementations similar or b, , added on time. have "elegant" (if possible) solution later on, need take care of implementation c, d, etc.

thanks!

actually getting instance invoke search() (and search) can done breaking abstract class:

public abstract class abstractrequest { public abstract void search(); } public abstract class abstractrequest<tresponsedata> : abstractrequest tresponsedata : iresponsedata { public abstract gooddata binddata(tresponsedata data); }

and can utilize original (non-generic) code:

var instances = t in assembly.getexecutingassembly().gettypes() t.isclass && typeof(abstractrequest).isassignablefrom(t) && t.getconstructor(type.emptytypes) != null select activator.createinstance(t) abstractrequest;

(old, busted solution)

i think (untested):

var abstractrequesttypes = (from t in assembly.getexecutingassembly().gettypes() t.isclass && typeof(iresponsedata).isassignablefrom(t) select typeof(abstractrequest<>).makegenerictype(t)).tolist(); var instancetypes = t in assembly.getexecutingassembly().gettypes() t.isclass && abstractrequesttypes.any(dt => dt.isassignablefrom(t));

this should cover in inheritance hierarchy, taking advantage of type constraint on abstractrequest.

c# inheritance abstract

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -