简体   繁体   English

System.InvalidCastException:“无法将类型为‘Concat2Iterator`1[System.Object]”的 object 转换为类型为“System.Object[]”。

[英]System.InvalidCastException: 'Unable to cast object of type 'Concat2Iterator`1[System.Object]' to type 'System.Object[]'.'

If I have this line of code in Visual Studio:如果我在 Visual Studio 中有这行代码:

object[] c = new[] { new object() }.Concat(new[] { new object() });

I get compiler error CS0266 :我收到编译器错误 CS0266

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'object[]'.无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“object[]”。 An explicit conversion exists (are you missing a cast?)存在显式转换(您是否缺少转换?)

Then I use the quick actions light bulb and select "Add explicit cast", it converts my code to this, adding (object[]) :然后我使用快速操作灯泡和 select “添加显式转换”,它将我的代码转换为此,添加(object[])

object[] c = (object[])new[] { new object() }.Concat(new[] { new object() }); 

No more compiler error;没有更多的编译器错误; but when I run the code, it throws a System.InvalidCastException:但是当我运行代码时,它会抛出 System.InvalidCastException:

'Unable to cast object of type 'Concat2Iterator`1[System.Object]' to type 'System.Object[]'.' “无法将类型为‘Concat2Iterator`1[System.Object]”的 object 转换为类型为“System.Object[]”。

Why would VS suggest this fix if it cannot actually make the cast at runtime?如果 VS 无法在运行时实际进行转换,为什么它会建议此修复? VS is giving bad advice, basically, which could lead to unexpected bugs escaping. The code compiles, and could blow up unexpectedly because you trusted VS' suggested fix.基本上,VS 给出了错误的建议,这可能会导致意外错误 escaping。代码编译后可能会意外崩溃,因为您信任VS 的建议修复。

Concat is a LINQ method. Concat是一个 LINQ 方法。 If you look at the signature, you see it returns an IEnumerable.如果您查看签名,您会发现它返回一个 IEnumerable。 LINQ uses deferred execution. LINQ 使用延迟执行。 You will have to enumerate the results, or call ToArray or ToList to convert to a collection.您将必须枚举结果,或调用ToArrayToList以转换为集合。

If you only want to concat arrays though, you should have a look at How do I concatenate two arrays in C#?如果您只想连接 arrays,您应该看看如何在 C# 中连接两个 arrays? . .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:无法将类型为“ System.Object”的对象转换为类型为“ System.IO.StreamWriter” - System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'System.IO.StreamWriter' System.InvalidCastException:无法转换类型的对象 - System.InvalidCastException: Unable to cast object of type 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' NLua:无法将类型为Command []的对象转换为类型为System.Object []的对象 - NLua: Unable to cast object of type 'Command[]' to type 'System.Object[]' 无法将类型为“&lt;&gt; d__6”的对象强制转换为“System.Object []” - Unable to cast object of type '<>d__6' to type 'System.Object[]' 无法将&#39;System.Object []&#39;类型的对象强制转换为&#39;MyObject []&#39;,是什么给出的? - Unable to cast object of type 'System.Object[]' to 'MyObject[]', what gives? System.InvalidCastException:无法将类型为x的对象强制转换为y - System.InvalidCastException: Unable to cast object of type x to type y System.InvalidCastException: &#39;无法将类型为 &#39;ClassName&#39; 的对象转换为类型 InterfaceName&#39;。 - System.InvalidCastException: 'Unable to cast object of type 'ClassName' to type InterfaceName'.' 多维数组转换运行时错误---&gt;无法将类型为&#39;System.Object [,]&#39;的对象转换为&#39;System.String类型 - Multidimensional Array Casting Runtime Error ---> unable to cast object of type 'System.Object[,]' to type 'System.String System.InvalidCastException:“无法将“System.Random”类型的对象转换为“System.IConvertible”类型。” - System.InvalidCastException: "Unable to cast object of type 'System.Random' to type 'System.IConvertible'."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM