以下抛出 InvalidCastException.
IEnumerable<int> list = new List<int>() { 1 };
IEnumerable<long> castedList = list.Cast<long>();
Console.WriteLine(castedList.First());
为什么?
我使用的是 Visual Studio 2008 SP1.
I'm using Visual Studio 2008 SP1.
这很奇怪!有一篇博文 here 描述了 Cast<T>() 的行为如何在 .NET 3.5 和 .NET 3.5 SP1 之间发生变化,但它仍然没有解释InvalidCastException,如果您这样重写代码,您甚至会得到它:
That's very odd! There's a blog post here that describes how the behaviour of Cast<T>() was changed between .NET 3.5 and .NET 3.5 SP1, but it still doesn't explain the InvalidCastException, which you even get if you rewrite your code thus:
var list = new[] { 1 };
var castedList = from long l in list select l;
Console.WriteLine(castedList.First());
显然你可以自己做演员来解决这个问题
Obviously you can work around it by doing the cast yourself
var castedList = list.Select(i => (long)i);
这行得通,但它并没有首先解释错误.我尝试将列表转换为 short 和 float 并且抛出了相同的异常.
This works, but it doesn't explain the error in the first place. I tried casting the list to short and float and those threw the same exception.
编辑
那篇博文确实解释了为什么它不起作用!
That blog post does explain why it doesn't work!
Cast<T>() 是 IEnumerable 而不是 IEnumerable<T> 的扩展方法.这意味着当每个值到达它被强制转换的点时,它已经被装箱回 System.Object 中.本质上它正在尝试这样做:
Cast<T>() is an extension method on IEnumerable rather than IEnumerable<T>. That means that by the time each value gets to the point where it's being cast, it has already been boxed back into a System.Object. In essence it's trying to do this:
int i = 1;
object o = i;
long l = (long)o;
此代码会引发您得到的 InvalidCastException.如果您尝试将一个 int 直接转换为 long 就可以了,但是将一个装箱的 int 转换回 long 是行不通的.
This code throws the InvalidCastException you're getting. If you try to cast an int directly to a long you're fine, but casting a boxed int back to a long doesn't work.
当然是个怪人!
这篇关于令人费解的 Enumerable.Cast InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
有没有办法知道是否有人为您的网站添加了书签Is there a way to know if someone has bookmarked your website?(有没有办法知道是否有人为您的网站添加了书签?)
使用不同的 .Net 语言?Use of Different .Net Languages?(使用不同的 .Net 语言?)
是否有执行 Excel NORMINV 函数的 C# 库?Is there a C# library that will perform the Excel NORMINV function?(是否有执行 Excel NORMINV 函数的 C# 库?)
确定 ASP.NET 站点的“活动"用户数Determining an #39;active#39; user count of an ASP.NET site(确定 ASP.NET 站点的“活动用户数)
从 C# 中的加权列表中选择 x 个随机元素(无需替换Select x random elements from a weighted list in C# (without replacement)(从 C# 中的加权列表中选择 x 个随机元素(无需替换))
跟踪当前在线用户的最佳方式Best way to keep track of current online users(跟踪当前在线用户的最佳方式)