我具有以下一组类和一个接口:
class ReturnTypeBase { }
class ReturnTypeChild : ReturnTypeBase { }
interface interface {
ReturnTypeBase Method();
}
我想用返回ReturnTypeChild
而不是ReturnTypeBase
实例的方法来实现接口。我实际上可以编写代码,并且由于ReturnTypeChild是ReturnTypeBase,所以我希望一切正常,但是编译器认为此接口的这种实现无效:
class SomeConcreteImplementation : IInterface {
public ReturnTypeChild Method();
}
从逻辑上讲,这似乎是正确的,但是我一定想念一些东西。是否有逻辑上的原因导致该实现无效或纯粹是技术限制?我将如何实现这样的实现(例如,为了进行单元测试,我在其中使用类实例而不是接口实现,并且我想知道该方法的实际返回类型,以避免必须将其全部转换为断言的时间)
P.S。通用接口不是一种选择,因为我必须能够将这些接口实现存储在集合中。