【发布时间】:2012-01-25 10:09:59
【问题描述】:
此代码生成两个编译时错误:
private void DoSomething()
{
List<List<Foo>> myFoos = GetFoos();
UseFoos(myFoos);
}
private void UseFoos(IEnumerable<IEnumerable<Foo>>)
{
}
The best overloaded method match for 'NameSpace.Class.UseFoos(System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<Foo>>)' has some invalid arguments
和
Argument 1: cannot convert from 'System.Collections.Generic.List<System.Collections.Generic.List<Foo>>' to 'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<Foo>>'
投射到IEnumberable<List<Foo>> 不是问题。强制转换失败类型的内部 List 组件有什么不同?
【问题讨论】:
-
你是 C# 4.0 之前的版本吗? stackoverflow.com/questions/1280332/… 和 stackoverflow.com/questions/730401/upcasting-and-generic-lists 可能是相关的,如果不是完全的欺骗。
-
您确定您使用的是 C# 4.0?
-
@AakashM:正确我使用的是 3.5。
-
@AakashM 查看这些示例中的 3.5 解决方法;除非/直到我真的需要支持内部容器不是
List<>之外的东西,我想我会通过的。实现都属于丑陋和明显性检查。