【发布时间】:2012-08-14 15:22:19
【问题描述】:
我有接口IChild 和IParent。 IParent 有一个成员是 List<IChild>。
我希望有实现IParent 的类,其中每个类都有一个实现IChild 的成员:
public interface IChild
{
}
public interface IParent
{
List<IChild> a { get; set; }
}
public class ChildA : IChild
{
}
public class ChildB : IChild
{
}
public class ParentA : IParent
{
public List<ChildA> a { get; set; }
}
public class ParentB : IParent
{
public List<ChildB> a { get; set; }
}
但是,这段代码不会编译。错误是:
`MyApp.Data.ParentA` does not implement interface member `MyApp.Data.IParent.a`.
`MyApp.Data.ParentA.a` cannot implement `MyApp.Data.IParent.a` because it does not have
the matching return type of `System.Collections.Generic.List<MyApp.Data.IChild>`.
【问题讨论】:
-
将
List属性声明为ILists 会更明智,它们的限制性较小,但提供相同的功能。不过对这个问题没有帮助。 -
那么他就可以做到
IEnumerable。这可能不是要求 -
@Jodrell
IList不提供与List相同的功能。 -
@Suncat2000,你是对的,但是,
IList、ICollection(他们的只读对应物)或IEnumerable可能是一个较小的承诺,可以满足所需的功能级别。跨度>
标签: c# generics inheritance interface