简体   繁体   English

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

[英]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. 我不确定IConcreteControllerIConcreteView接口是否能取得很大的成就,我将完全摆脱它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型 &#39;&#39; 不能用作泛型类型或方法 &#39;&#39; 中的类型参数 &#39;T&#39;。 没有从 &#39;&#39; 到 &#39;&#39; 的隐式引用转换 - The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to '' 该类型不能用作泛型类型或方法中的类型参数“TTo”。 没有隐式引用转换 - The type cannot be used as type parameter 'TTo' in the generic type or method. There is no implicit reference conversion 该类型不能在泛型类型或方法中用作类型“T”。 没有隐式的引用转换 - The type cannot be used as type 'T' in the generic type or method. There is no implicit reference conversion from to 该类型不能在泛型类型或方法'BaseController <T>'中用作类型参数'T'。没有隐含的参考 - The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A” 类型“int”不能用作泛型方法中的类型参数“T”。 'int' 没有拳击转换 - The type 'int' cannot be used as type parameter 'T' in the generic method. There is no boxing conversion from 'int' 通用方法-类型不能用作类型参数 - Generic method - Type cannot be used as Type Parameter 该类型不能用作通用类型或方法中的类型参数 - The type cannot be used as type parameter in the generic type or method 类型&#39;&#39;不能在通用类型或方法中用作类型参数&#39;T&#39; - The type '' cannot be used as type parameter 'T' in the generic type or method 类型“ XXX”不能用作通用类型或方法中的类型参数“ T” - The type 'XXX' cannot be used as type parameter 'T' in the generic type or method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM