c# - 将子类分配给接口(interface),涉及泛型和协变

标签 c# .net covariance contravariance

我定义了3个接口(interface):

public interface IManufacturerInput
{
}
public interface IManufacturerOutput
{
}
public interface IManufacturerApi<in S, out T>
    where S : IManufacturerInput
    where T : IManufacturerOutput
{
    T Calculate(S);
}

我定义了一个特定的制造商:

public class ManufacturerAInput : IManufacturerInput
{
}
public class ManufacturerAOutput : IManufacturerOutput
{
}
public class ManufacturerAApi : IManufacturerApi<ManufacturerAInput, ManufacturerAOutput>
{
     public ManufacturerAOutput Calculate(ManufacturerAInput)
     {
         return null;
     }
}

在 Main() 中,我创建了一个ManufacturerAApi,并尝试将其分配给 IManufacturerApi。

IManufacturerApi<IManufacturerInput, IManufacturerOutput> api = new ManufacturerAApi();

但是失败了。错误信息说(只是抽象意思):

Can't convert from ManufacturerAApi to IManufacturerApi<IManufacturerInput, IManufacturerOutput>

那么有什么办法可以让我的作业顺利完成吗?提前致谢。

最佳答案

您提出的建议不是类型安全的。让我们更改类型的名称以使问题更清楚:

public interface IPetFood { }
public interface IPetSound { }

public interface IPetCage<in S, out T>
    where S : IPetFood
    where T : IPetSound
{
    T Feed(S s);
}

public class DogFood : IPetFood { }
public class CatFood : IPetFood { }
public class Bark : IPetSound { }

public class DogCage : IPetCage<DogFood, Bark>
{
    public Bark Feed(DogFood input)
    {
        return new Bark();
    }
}

现在假设这是合法的:

IPetCage<IPetFood, IPetSound> api = new DogCage();

然后我们可以执行以下操作:

api.Feed(new CatFood()); //oops we've just given the dog some catfood.

分配将不起作用,因为 S 是逆变的,这意味着任何可能传递到 api.FeedIPetFood 都需要是DogFood 的子类型,而你有相反的情况; IPetFoodDogFood 的超集。

关于c# - 将子类分配给接口(interface),涉及泛型和协变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112852/

相关文章:

java - 将泛型列表转换为子类型(不安全逆变)

excel - 使用 Excel 计算协方差矩阵

c# - .Net/SQL 在 datetime2 上插入和选择不匹配

c# - 毛玻璃窗上的非客户绘画

c# - ASP.NET MVC : How to reliably get the real URL of the current request?

c# - log4net log4net.Util.PatternString 从代码配置

c# - 使用 C# 在 SQL 中使用 UPDATE 命令时出错

c# - 在 AspNet WebApi 帮助页面中生成模型描述

c# - 使用 Linq 的具有预定义类型的 GroupBy

scala - 协变类型 T 出现在不变位置