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


- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01