令人费解的 Enumerable.Cast InvalidCastException

时间:2023-02-02
本文介绍了令人费解的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 C# 中重新抛出 InnerException 而不会丢失堆栈跟踪? 下一篇:如何在不等待的情况下在 C# 中安全地调用异步方法

相关文章

最新文章