[英]The type '' cannot be used as type parameter '' in the generic type or method ''. There is no implicit reference conversion from '' to ''
I am trying to implement some generic in View - Controller classes. 我正在尝试在View-Controller类中实现一些泛型。 My "framework" looks like that:
我的“框架”如下所示:
Controllers: 控制器:
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
}
Views: 观看次数:
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
}
But I get an error in IConcreteView file: 但是我在IConcreteView文件中收到错误:
The type 'IConcreteController' cannot be used as type parameter 'TController' in the generic type or method 'BaseView'.
在通用类型或方法“ BaseView”中,类型“ IConcreteController”不能用作类型参数“ TController”。 There is no implicit reference conversion from 'IConcreteController' to 'IController'.
从“ IConcreteController”到“ IController”没有隐式引用转换。
What should I fix to get this "template" to work? 我应该如何解决才能使此“模板”正常工作?
Update: 更新:
As Ned Stoyanov suggest: 正如内德·斯托亚诺夫(Ned Stoyanov)所说:
I chenged ConcreteView to: 我将ConcreteView设置为:
public class ConcreteView<TController> : BaseView<TController>, IConcreteView where TController : IController<IView>
and IConcreteView to: 和IConcreteView可以:
public interface IConcreteView
and now it works. 现在可以了。
I think the problem is with your definition of IConcreteView
我认为问题在于您对
IConcreteView
的定义
public abstract class IConcreteView : BaseView<IConcreteController>
It iherits from BaseView<IConcreteController>
and the class BaseView<TController>
has a constraint that TController
must be an IController<IView>
它从iherits
BaseView<IConcreteController>
和类BaseView<TController>
具有一个约束TController
必须是IController<IView>
BaseView<TController> : IView where TController : IController<IView>
But IConcreteController
is not an IController<IView>
, it's just a stand alone interface, hence the error message. 但是
IConcreteController
不是IController<IView>
,它只是一个独立的接口,因此会出现错误消息。
I am not sure that the interfaces IConcreteController
and IConcreteView
are achieving much, I'd just get rid of them altogether. 我不确定
IConcreteController
和IConcreteView
接口是否能取得很大的成就,我将完全摆脱它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.