接口继承和通用接口强制显式强制转换? [英] Interface inheritance and generic interfaces force explicit casts?

查看:114
本文介绍了接口继承和通用接口强制显式强制转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更复杂的问题,但我把它简化为以下简单的例子:

  using System ; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

namespace Sandbox
{
class Program
{
static void Main(string [] args)
{
IFactory< IProduct>工厂=新工厂();
}
}

class工厂:IFactory< Product>
{

}

class产品:IProduct
{

}

接口IFactory< ; T> T:IP产品
{

}

接口IP产品
{

}
}

一切都很好,很花哨......除了我得到这个错误。



错误1无法将类型 Sandbox.Factory 隐式转换为 Sandbox.IFactory< Sandbox.IProduct> 。存在明确的转换(你是否缺少转换?)c:\ ~~ \Program.cs 12 42 Sandbox



任何人都愿意提供洞察,案子?我相信Jon Skeet或Eric Lippert可以在心中解释为什么会出现这种情况,但必须有人不仅能够理解为什么这是无法推断的,而是可以解释如何最好地解决这种情况。



后续问题这里

解决方案

这是因为工厂 IFactory< Product> 并且您将它分配给的是 IFactory<因为 IFactory 不是



试着制作 IFactory< rel =nofollow> covariant ,你不能将泛型转换为泛型的泛型。 ;

编辑

$ b $ ,这应该使下列分配工作。 b
@Firoso,在您的工厂界面中,您尝试创建一个列表,您可以在其中写入。如果你的界面是协变的,你不能写任何东西,因为以下几点:

  List< object> list = new List< string>(); //这是不可能的方式
list.Add(new {}); //因为底层类型是List< string>

你应该忽略你的情况下的协方差并且创建赋值给 IFactory<产品> 代替或改变工厂继承 IFactory< IProduct> ,我推荐后者,但由您决定


I have a much more complicated issue, but I've boiled it down to the following simple example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            IFactory<IProduct> factory = new Factory();
        }
    }

    class Factory : IFactory<Product>
    {

    }

    class Product : IProduct
    {

    }

    interface IFactory<T> where T : IProduct
    {

    }

    interface IProduct
    {

    }
}

All is well and dandy... except that I get this error.

Error 1 Cannot implicitly convert type Sandbox.Factory to Sandbox.IFactory<Sandbox.IProduct>. An explicit conversion exists (are you missing a cast?) c:\~~\Program.cs 12 42 Sandbox

Anyone willing to provide insight into why this is the case? I'm sure Jon Skeet or Eric Lippert could explain in a heartbeat why this is, but there has to be someone that not only understands WHY this can't be inferred, but can explain how best to solve this situation.

Follow up question here

解决方案

It is because Factory is an IFactory< Product> and what you are assigning it to is a IFactory< IProduct>, and since IFactory is not covariant, you cannot cast a generic of subtype to a generic of supertype.

Try making IFactory< out T>, which should make the following assignment work.

EDIT:

@Firoso, in your factory interface you are trying to create a list, in which you can write to. If your interface is covariant you can not write to anything, because of the following:

List<object> list = new List<string>(); //This is not possible by the way
list.Add(new {}); //Will fail here because the underlying type is List<string> 

You should ignore covariance in your case and just create assign to an IFactory<Product>instead or change factory to inherit IFactory<IProduct>instead, I recommend the latter but it is up to you

这篇关于接口继承和通用接口强制显式强制转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆