从理论上讲,我认为应该可以将IEnumerable转换为IEnumerable< TSource>但我得到'InvalidCastException',为什么? [英] Theoretically, I believe it should be possible to cast a IEnumerable to IEnumerable<TSource> but I Get 'InvalidCastException', Why?

查看:82
本文介绍了从理论上讲,我认为应该可以将IEnumerable转换为IEnumerable< TSource>但我得到'InvalidCastException',为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误消息:

未处理的异常:System.InvalidCastException:无法转换类型为'System的对象.Text.RegularExpressions.MatchCollection'在我的代码中输入'System.Collections.Generic.IEnumerable`1 [System.Text.RegularExpressions.Match]'。

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Text.RegularExpressions.MatchCollection' to type 'System.Collections.Generic.IEnumerable`1[System.Text.RegularExpressions.Match]'.

当我尝试指向'MatchCollection与通用IEnumerable< out T>。

in my code when I try to point to the 'MatchCollection with a generic IEnumerable<out T>.

所以:

System.String input;

System.Text.RegularExpressions.Regex regex =

System.Text.RegularExpressions.Regex regex =

new System.Text.RegularExpressions.Regex(" social");

new System.Text.RegularExpressions.Regex("social");

System.Text.RegularExpressions。 MatchCollection匹配= regex.Matches(输入);

System.Collections.Generic.IEnumerable< System.Text.RegularExpressions.Match> myEnumerableMatches =

匹配;

System.Text.RegularExpressions.MatchCollection matches = regex.Matches(input); System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Match> myEnumerableMatches = matches;

所以,我被告知System.Text。 RegularExpressions.MatchCollection不实现System.Collections.Generic.IEnumerable< out T>或(`1),但是你要写那个。我想这是一个很好的解释。但我的问题是,更多的理论上是
并且问了一个问题,"嗯,为什么要这么做?"

So, I've been told that System.Text.RegularExpressions.MatchCollection does not implement System.Collections.Generic.IEnumerable<out T> or (`1), however you want to write that. Which is a fine explanation, I guess. But my question is, more theoretical and ask the question, "Well, why does it have to?"

如果可以使用常规系统枚举MatchCollection。 Linq.IEnumerable将MatchCollection中的每个Match作为无类型对象返回,然后你就拥有了所有你真正需要使用的对象。您在内存中存储了对象数据
的地址。您在内存中的对象的地址开始。如果您作为程序员知道集合中对象的类型,为什么不能使用System.Collections.Generic.IEnumerable< T>枚举System.Linq.IEnumerable
,它自己返回普通的旧对象?

If the MatchCollection can be enumerated using a regular System.Linq.IEnumerable which returns each Match in the MatchCollection as an untyped object, then you have all you really need to use the object. You have the address in memory where the object data is stored. You have the address of the object in memory where it begins. And if you as the programmer know the type of the objects in the collection, why can't you use a System.Collections.Generic.IEnumerable<T> to enumerate the System.Linq.IEnumerable that returns plain old objects, yourself?

换句话说,似乎foreach循环没有任何麻烦枚举MatchCollection,因为它在使用它之前对每个对象执行强制转换。例如:

To say it another way, it seems that a foreach loop has no trouble at all enumerating the MatchCollection because it's performing a cast on each object before it uses it. For example:

foreach(System.Text.RegularExpressions.Match m in matches) { // do whatever here };

但我不能这样做:

System.Text.RegularExpressions.MatchCollection matches = regex.Matches(input); System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Match> myEnumerableMatches = matches;

myEnumerableMatches = matches;

myEnumerableMatches = matches;

IEnumerable类型< Match>知道它指向的集合中的类型,因为我告诉过它。为什么这是一个问题?

The IEnumerable of type <Match> know's the types in the collection it is pointing to because I've told it. Why is this a problem?




推荐答案

正如您在此处所见,
MatchCollection
是一个相当老的类,只实现ICollection接口而不是IEnumerable< T> ;

As you can see here, MatchCollection is a rather old class that only implements ICollection interface but not IEnumerable<T>.

您无法转换对象本身及其基本类型尚未实现该接口的界面。 .GetEnumerator()的返回值的类型将不是IEnumerator< T>。

You can't cast object which itself and it's base types haven't implement an interface to that interface. The type for return value of .GetEnumerator() will not be IEnumerator<T>.

泛型的全部意义在于消除装箱/拆箱的成本。如果它需要转换才能工作,你应该返回并使用IEnumerable代替并省去所有麻烦。

The whole point of Generics is to eliminate the cost of boxing/unboxing. If it would require casting to work, you ought to go back and use IEnumerable instead and save all the trouble.


这篇关于从理论上讲,我认为应该可以将IEnumerable转换为IEnumerable&lt; TSource&gt;但我得到'InvalidCastException',为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆