本文介绍了无法转换List< KeyValuePair< ...,...>>到IEnumerable< object>吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试这样的操作时获取InvalidCastException:

Getting an InvalidCastException when trying something like this :

IEnumerable<object> test = (IEnumerable<object>)new List<KeyValuePair<string, int>>();

但是,这确实可行:

IEnumerable<object> test = (IEnumerable<object>)new List<Dictionary<string, int>>();

那么最大的区别是什么?为什么不能将KeyValuePair转换为对象?

So what's the big difference? Why can a KeyValuePair not be converted to an object?

更新:我可能应该指出这确实有效:

Update: I should probably point out that this did work:

object test = (object)KeyValuePair<string,string>;

推荐答案

因为它是结构而不是类: http://msdn.microsoft.com/zh-cn/library/5tbh8a42.aspx

Because it is a struct, and not a class : http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx

这篇关于无法转换List&lt; KeyValuePair&lt; ...,...&gt;&gt;到IEnumerable&lt; object&gt;吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:30