我有一个通用接口层次结构,它描述了其他通用类型的一些控制器,我很难在脑海中澄清为什么特定的强制转换场景无效。
简化代码如下;
// 'DTO' interfaces
public interface IBase
{ }
public interface IDerived : IBase
{ }
// 'DTOs'
public class Base : IBase
{ }
public class Derived : Base, IDerived
{ }
// controller interfaces
public interface IBaseController<T> where T : class, IBase
{ }
public interface IDerivedController : IBaseController<IDerived>
{ }
// controllers
public class BaseController<T> : IBaseController<T>
where T : class, IBase
{ }
public class DerivedController : BaseController<IDerived>, IDerivedController
{ }
现在,我遇到的问题是这样的;
IDerivedController x = new DerivedController();
bool is1 = x is IDerivedController; // true
bool is2 = x is IBaseController<IDerived>; // true
bool is3 = x is IBaseController<IBase>; // false ???
最后一行是我的困惑所在。控制器接口与“DTO”正确关联。但不是两者都在一起......?