他のジェネリック型の一部のコントローラーを記述するジェネリック インターフェイス階層があり、特定のキャスト シナリオが有効でない理由を明確にするのに苦労しています。
簡略化されたコードは次のとおりです。
// '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」が関連付けられます。しかし、両方一緒ではありません... ?