类型“”不能用作泛型类型或方法“”中的类型参数“”.没有从 '' 到 '' 的隐式引用转

本文关键字:引用 类型参数 不能 泛型类型 类型 方法 | 更新日期: 2023-09-27 18:36:04

我正在尝试在View - 控制器类中实现一些通用。我的"框架"看起来像这样:

控制器:

public interface IController<TView> where TView : IView
{
    // some generic fields / methods definitions
}
public abstract class BaseController<TView> : IController<TView> where TView : IView
{
    // some generic fields / methods implementation
}
public interface IConcreteController
{
    // some specific fields / methods definition
}
public class ConcreteController<TView> : BaseController<TView>, IConcreteController where TView : IView
{
    // specific implementation
}

视图:

public abstract class IView : UserControl // MARKER
{
}
public class BaseView<TController> : IView where TController : IController<IView>
{
    // some generic fields / methods implementation
}
public abstract class IConcreteView : BaseView<IConcreteController>
{
    // some specyfic fields methods definition
}
public class ConcreteView : IConcreteView
{
    // some specific fields methods implementation
}

但是我在IConcreteView文件中出现错误:

类型"IConcreteController"不能用作类型参数 泛型类型或方法"BaseView"中的"TController"。 没有来自"IConcreteController"的隐式引用转换 到"主计长"。

我应该修复什么才能让这个"模板"工作?

更新:

正如内德·斯托亚诺夫所说

我把ConcreteView带到了:

public class ConcreteView<TController> : BaseView<TController>, IConcreteView where TController : IController<IView>

IConcreteView到

public interface IConcreteView

现在它起作用了。

类型“”不能用作泛型类型或方法“”中的类型参数“”.没有从 '' 到 '' 的隐式引用转

我认为问题在于您对IConcreteView的定义

public abstract class IConcreteView : BaseView<IConcreteController>

它与BaseView<IConcreteController>和类BaseView<TController>有一个约束,即TController必须是IController<IView>

BaseView<TController> : IView where TController : IController<IView>

但是IConcreteController不是IController<IView>,它只是一个独立的界面,因此出现错误消息。

我不确定IConcreteControllerIConcreteView的接口是否取得了多大成就,我会完全摆脱它们。