c# - Implement a generic class where the definition is on a base interface type but the implementation is on an interface derived from that base? -
is possible in c# implement generic class definition on base interface type implementation on interface derived base?
i have base type core functionality need 2 different variations on type depending on if process working data data or integer data.
i kluge base type have both data types i'd rather not.
example of problem:
public interface ia {} public interface ib : ia {} public class ca : ia {} public class cb : ib {} public interface ic<t1> t1 : ia { } public class c<tia> : ic<tia> tia : ia {} public class thing { public void some() { ia = new cb(); // fine ib of type ia c<ib> b = new c<ib>(); // fine - c<ib> y = new c<ia>(); // shouldn't work - doesn't work c<ia> x = new c<ib>(); // though ib of type ia not acceptable } } cannot implicitly convert type 'classlibrary1.c<classlibrary1.ia>' 'classlibrary1.c<classlibrary1.ib>' // makes sense cannot implicitly convert type 'classlibrary1.c<classlibrary1.ib>' 'classlibrary1.c<classlibrary1.ia>' // should work - ib derives ia
if can't implement generic on derived interfaces have lot of reworking on existing application. there kind of simple way implement this?
if declare type parameter t1
of interface ic
covariant
public interface ic<out t1> t1 : ia { }
then can assign instance of c<ib>
variable of type ic<ia>
ic<ia> x = new c<ib>(); // works
but i'm not sure if answers question...
Comments
Post a Comment