我正在尝试在View - Controller类中实现一些泛型。我的#34;框架"看起来像那样:
控制器:
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 文件中收到错误:
类型&#39; IConcreteController&#39;不能用作类型参数 &#39; TController&#39;在通用类型或方法&#39; BaseView&#39;。 来自&#39; IConcreteController&#39;没有隐式引用转换。 到&#39; IController&#39;。
我应该怎样修复这个&#34;模板&#34;工作?
更新
Ned Stoyanov 建议:
我将 ConcreteView 变为:
public class ConcreteView<TController> : BaseView<TController>, IConcreteView where TController : IController<IView>
和 IConcreteView :
public interface IConcreteView
现在可行。
答案 0 :(得分:2)
我认为问题在于您对IConcreteView
public abstract class IConcreteView : BaseView<IConcreteController>
它来自BaseView<IConcreteController>
,而BaseView<TController>
类的约束TController
必须是IController<IView>
BaseView<TController> : IView where TController : IController<IView>
但是IConcreteController
不是IController<IView>
,它只是一个独立的界面,因此是错误信息。
我不确定接口IConcreteController
和IConcreteView
取得了多大成就,我只是完全摆脱它们。