铸造包含通用词典通用词典 [英] Casting a generic dictionary containing a generic dictionary

查看:158
本文介绍了铸造包含通用词典通用词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

var someConcreteInstance = new Dictionary<string, Dictionary<string, bool>>();

和我想它转换为一个接口的版本,即:

and I wish to cast it to an interface version, i.e.:

someInterfaceInstance = (IDictionary<string, IDictionary<string, bool>>)someConcreteInstance;



someInterfaceInstance是公共财产:

'someInterfaceInstance' is a public property:

IDictionary<string, IDictionary<string, bool>> someInterfaceInstance { get; set; }

这编译正确,但引发运行时铸造的错误。

This compiles correctly, but throws a runtime casting error.

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]]'.



我在想什么? (与嵌套泛型类型/产权问题?)

What am I missing? (Problems with the nested generic type/Property?)

推荐答案

的IDictionary 不支持协方差。

看这里
IDictionary的< TKEY的,TValue>在.NET 4中不是协变

这篇关于铸造包含通用词典通用词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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