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
Post a Comment