c# - Adding inherited generics to a generic list -


i'm trying add objects list, objects generics inherit interface.

base class listeners:

public class listener<t> : ilistener<t>     t : icaller {      public listener()  {      }      public type getcallertype() {         return typeof(t);     }      public virtual void received(connection connection, t obj) {} } 

example listener:

public class clientpacketlistener<t> : listener<t>     t : packet { } 

i want add them list:

listener<icaller> listeners = new listener<icaller>(); 

in case, packet inherits icaller, should able cast down base types. error can't cast however. i've looked @ other questions, work generic <t>, not listener class. there way work or not?

i think can resolve problem if change ilistener<t> declaration ilistener<out t> making t covariant.

it should allow this

ilistener<icaller> listener = new clientpacketlistener<packet>(); 

for more information read msdn out (generic modifier).


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -