我定义了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.Feed
的 IPetFood
都需要是DogFood
的子类型,而你有相反的情况; IPetFood
是 DogFood
的超集。
关于c# - 将子类分配给接口(interface),涉及泛型和协变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112852/