【发布时间】:2011-07-22 23:18:40
【问题描述】:
我有
public interface IFoo
{
IEnumerable<IThingy> Thingies{get;}
}
我希望以后能够做到
class Thing1 : IThingy
{
...
}
class ImplementFoo : IFoo
{
List<Thing1> m_things;
IEnumerable<IThingy> Thingies {get {return m_things;}}
}
ImplementFoo.Thingies 返回 Thing1(即 IThings)的 IList(即 IEnumerable)。所以理论上这段代码应该可以工作,但事实并非如此。 VS 建议在 getter 中进行强制转换;编译但在运行时失败。我对 c# 4 的协方差期望过高吗?
VS 2010 -> Silverlight 4. 这是编译错误
无法将类型“
System.Collections.Generic.List<MyProj.Column>”隐式转换为“System.Collections.Generic.IEnumerable<MyProj.IColumn>”。存在显式转换(您是否缺少演员表?)
编辑:人们告诉我这应该可以,但在 SL4 中不起作用
【问题讨论】:
-
您的项目是否面向 .NET 4?
-
.net 4 是肯定的。让我再试一次。实际上是 Silverlight 4
标签: c# covariance