Puzzling Enumerable.Cast InvalidCastException(令人费解的 Enumerable.Cast InvalidCastException)
问题描述
以下抛出 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:令人费解的 Enumerable.Cast InvalidCastException
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01