【发布时间】:2010-11-15 20:54:41
【问题描述】:
我正在尝试解决 C# 中缺乏对返回类型协方差的支持的问题,如this 问题及其前两个答案中所述。在大多数情况下,我在设置演员表时没有任何问题,但我使用对象/接口列表的一个属性阻碍了我的努力。
我需要做什么才能使IFoo.manyBars 的演员表工作?
public interface IBar
{
}
public interface IFoo
{
IBar aBar { get; set; }
IEnumerable<IBar> manyBars { get; set; }
}
class CBar : IBar
{
}
class CFoo : IFoo
{
public CBar aBar { get; set; }
//this cast works
IBar IFoo.aBar
{
get { return aBar; }
set { aBar = (CBar)value; }
}
public List<CBar> manyBars { get; set; }
//the compiler can't cast either of these
List<IBar> IFoo.manyBars
{
get { return (List<IBar>)manyBars; }
set { manyBars = (List<CBar>)value; }
}
}
【问题讨论】: