转换包含通用字典的通用字典
我有:
var someConcreteInstance = new Dictionary<string, Dictionary<string, bool>>();
并且我希望将其转换为接口版本,即:
someInterfaceInstance = (IDictionary<string, IDictionary<string, bool>>)someConcreteInstance;
“someInterfaceInstance”是公共属性:
IDictionary<string, IDictionary<string, bool>> someInterfaceInstance { get; set; }
这可以正确编译,但会引发运行时转换错误。
Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.Dictionary`2[System.String,System.Boolean]]' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Collections.Generic.IDictionary`2[System.String,System.Boolean]]'.
我缺少什么? (嵌套泛型类型/属性有问题吗?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他答案是正确的,但为了清楚地说明为什么这是非法的,请考虑以下事项:
没有凡人可以阻止您将老虎放入 IAnimals 字典中。但该词典实际上仅限于包含长颈鹿。
出于同样的原因,您也不能走其他路:
现在您将老虎放入长颈鹿类型的变量中。
仅当编译器能够证明不会出现此类情况时,通用接口协变才在 C# 4 中合法。
The other answers are right, but just to be crystal clear as to why this is illegal, consider the following:
No mortal hand can stop you putting a tiger into a dictionary of IAnimals. But that dictionary is actually constrained to contain only giraffes.
For the same reason you can't go the other way either:
Now you're putting a tiger in a variable of type giraffe.
Generic interface covariance is only legal in C# 4 if the compiler can prove that situations like this cannot arise.
IDictionary
不支持协方差。看这里在 .NET 4 中不是协变的
IDictionary
IDictionary
does not support covariance.Look here
IDictionary<TKey, TValue> in .NET 4 not covariant
您最多能做的是,
您的内部字典不能在这里以不同方式引用(或通过强制转换)的原因是,虽然
Dictionary
是一个IDictionary
,并非所有IDictionary
对象都是Dictionary
对象。因此,获得纯接口转换似乎允许您将其他>
对添加到原始集合,而显然可能存在类型违规原始对象。因此,这是不支持的。The most you will be able to do is
The reason your inner dictionary cannot be referenced differently here (or via a cast) is that while
Dictionary<string, bool>
is anIDictionary<string, bool>
, not allIDictionary
objects will beDictionary
objects. As such, obtaining a pure interface cast would then seemingly allow you to add other<string, IDictionary<string, bool>>
pairs to the original collection, when clearly there could be type violations for the original object. Therefore, this is not supported.