C# casting an inherited Generic interface

此生再无相见时 提交于 2019-11-27 22:10:25

The problem is the generic type parameter. If you make the interface parameter covariant then the cast will work.

This is accomplished by adding the out keyword, like so:

interface IPresenter<out V> where V : IView
{
    void PresBlah();

}

You can learn more about how this works with the following MSDN article: Covariance and Contravariance in Generics. The section Generic Interfaces with Covariant Type Parameters specifically applies to your question.

Update: Make sure you check the comments between @phoog and me. If your actual code accepts a V as an input, you will be unable to make it covariant. The referenced article and @phoog's answer explains this case in further detail.

CPresenter<CView> is not an IPresenter<IView>, just as List<int[]> is not an IList<IEnumerable>.

Think about it. If you could get an IList<IEnumerable> reference to a List<int>, you could add a string[] to it, which would have to throw an exception. The whole point of static type checking is to prevent the compilation of such code.

If the interface allows, you could declare the type parameter as covariant (IPresenter<out V> where V : .... Then the interface would behave more like IEnumerable<out T>. This is only possible if the type parameter is never used in an input position.

To go back to the List<int[]> example, it is safe to treat it as an IEnumerable<IEnumerable>, because you can't add anything to an IEnumerable<T> reference; you can only read things out of it, and, in turn, it is safe to treat an int[] as an IEnumerable, so all is well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!