[英]Generic types. There is no boxing conversion or type parameter conversion
I have the following structure of my code : 我的代码具有以下结构:
public interface ICode { }
public interface IPreparedCode<TCode>
where TCode : ICode { }
public interface ILayer<TPreparedCode>
where TPreparedCode : IPreparedCode<ICode> { }
public interface IContext<TCode, TPreparedCode, TLayer>
where TCode : ICode
where TPreparedCode : IPreparedCode<TCode>
where TLayer : ILayer<TPreparedCode> { }
and i have error "There is no boxing conversion or type parameter conversion from TPreparedCode to IPreparedCode<ICode>". 并且我有错误“没有从TPreparedCode到IPreparedCode <ICode>的装箱转换或类型参数转换”。
The generic parameter for ILayer
is constrained to be IPreparedCode<ICode>
, but the generic parameter you are giving it, is TPreparedCode
, which is constrained to IPreparedCode<TCode>
. ILayer
的通用参数被限制为IPreparedCode<ICode>
,但是您提供的通用参数是TPreparedCode
,它被限制为IPreparedCode<TCode>
。 Notice the difference? 注意区别吗?
IPreparedCode<TCode>
can't be converted to IPreparedCode<ICode>
. IPreparedCode<TCode>
无法转换为IPreparedCode<ICode>
。
To fix this, you can add an extra generic parameter TCode
to ILayer
, and constrain TPreparedCode
to IPreparedCode<TCode>
instead.: 要解决此问题,您可以向
ILayer
添加一个额外的通用参数TCode
,并将TPreparedCode
约束为IPreparedCode<TCode>
。
public interface ICode { }
public interface IPreparedCode<TCode>
where TCode : ICode { }
public interface ILayer<TPreparedCode, TCode>
where TCode : ICode
where TPreparedCode : IPreparedCode<TCode> { }
public interface IContext<TCode, TPreparedCode, TLayer>
where TCode : ICode
where TPreparedCode : IPreparedCode<TCode>
where TLayer : ILayer<TPreparedCode, TCode> { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.