首页 > 解决方案 > 将接口转换为泛型类(继承)

问题描述

我在代码中有以下结构:

AController<S> : IScreenController where T : IProperties {}

FOOTypeControllers<T> : AController<T> where T : IScreenProperties
{
}

MyFooTypeController : FOOTypeControllers<MyProperties>
{
}

class MyProperties : IProperties {}

我想要做的是将 MyFooTypeController 类型转换为 FOOTypeControllers 但我得到一个无效的转换错误。为什么不允许这样做,因为它确实从类继承。

这正是我试图投射的方式:

void MyFucntion(IScreenController screenController)
{
  FOOTypeControllers<IProperties> screenControl = (FOOTypeControllers<IProperties>) screenController
}

FOOTypeControllers 的每个子类也将使用 Iproperties 的不同子类。

标签: c#genericsinheritancepolymorphism

解决方案


推荐阅读