c# - How to find all the interfaces that extend a base interface on a given object? -


i'm trying create crude/basic event subscribe/publish system experiment.

i created base ieventlistener interface, generic ieventlistener<t> : ieventlistener interface on top of that, has function onevent(t eventparam)

i created test class implemented ieventlistener<string> , ieventlistener<int>

i thought passing through following:

dictionary<type, list<object>> _listenersbytype = new dictionary<type, list<object>>();  foreach(type interfacetype in listener.gettype().getinterfaces()) {     if(interfacetype ieventlistener)     {         addsubscriber(interfacetype.getgenerictypedefinition(), listener);     } } 

i create of event types objects cast , publish events to. however, when stepping through. see loop interfaces, can see type name "ieventlistener" condition never true, , never adds listener dictionary.

pastebin of full code

through means of unsure (i poked around in debugger), fixes it:

foreach(type interfacetype in listener.gettype().getinterfaces()) {     if(interfacetype.getinterfaces().contains(typeof(ieventlistener)))     {         addsubscriber(interfacetype.getgenericarguments()[0], listener);     } } 

but cannot tell why have check interfaces, of interface. or why have call interfacetype.getgenericarguments()[0] instead of interfacetype.getgenerictypedefinition().

part of me feels code bad , i've got design issue here. never expect solution so... ugly.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -