无法转换List< KeyValuePair< ...,...>>到IEnumerable< object>吗? [英] Can't convert List<KeyValuePair<...,...>> to IEnumerable<object>?

查看:39
本文介绍了无法转换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>;

推荐答案

因为它是结构而不是类:

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;吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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