Find implemented types from Generic interface in c# -
public class accountcreatedevent : eventbase{} public class accounthandler : ieventhandler<accountcreatedevent> { public void handle(accountcreatedevent event) { } }
this handler class , want class c# code. want list of implemented classes ieventhandler type.
public class account { public void onaccountcreated(eventbase accountcreatedevent) { var handler = typeof(ieventhandler<>); var events = appdomain.currentdomain.getassemblies() .selectmany(s => s.gettypes()) .where(p => handler .isassignablefrom(p) && handler.isgenerictype); } }
but var events
returning
{name = "ieventhandler`1" fullname = "project1.ieventhandler`1"}
as suggested praveen, using how generic interfaces handled
type interfacetype = typeof(ieventhandler<>); assembly mscorlib = typeof(system.int32).assembly; assembly system = typeof(system.uri).assembly; assembly systemcore = typeof(system.linq.enumerable).assembly; var events = appdomain.currentdomain.getassemblies() // skip 3 common assemblies of microsoft .where(x => x != mscorlib && x != system && x != systemcore).toarray(); .selectmany(s => s.gettypes()) .where(p => p.getinterfaces().any(i => i.isgenerictype && i.getgenerictypedefinition() == interfacetype)).toarray();
note speedup thing little, i'm skipping 3 common assemblies of microsoft. skipping ms assemblies little more complex (it done through publickeytoken, don't think very-very-good idea... publickeytoken 64bits, , isn't guaranteed unique... , retrieving full publickey of assembly pain)
Comments
Post a Comment