将子类分配给接口,涉及泛型和协变

时间:2017-03-30 08:49:23

标签: c# .net covariance contravariance

我定义了3个接口:

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>

那么有什么方法可以让作业完成吗?提前谢谢。

1 个答案:

答案 0 :(得分:4)

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

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是逆变的,这意味着传入IPetFood的任何可能的api.Feed都需要是DogFood的子类型,并且您拥有相反; IPetFoodDogFood的超集。