nic*_*s13 8 c# generics dictionary casting nested
我有:
var someConcreteInstance = new Dictionary<string, Dictionary<string, bool>>();
Run Code Online (Sandbox Code Playgroud)
我希望将它转换为接口版本,即:
someInterfaceInstance = (IDictionary<string, IDictionary<string, bool>>)someConcreteInstance;
Run Code Online (Sandbox Code Playgroud)
'someInterfaceInstance'是一个公共属性:
IDictionary<string, IDictionary<string, bool>> someInterfaceInstance { get; set; }
Run Code Online (Sandbox Code Playgroud)
这正确编译,但抛出运行时转换错误.
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]]'.
Run Code Online (Sandbox Code Playgroud)
我错过了什么?(嵌套泛型类型/ Property的问题?)
Eri*_*ert 12
其他答案是正确的,但为了清楚地知道这是非法的,请考虑以下事项:
interface IAnimal {}
class Tiger : IAnimal {}
class Giraffe : IAnimal {}
...
Dictionary<string, Giraffe> d1 = whatever;
IDictionary<string, IAnimal> d2 = d1; // suppose this were legal
d2["blake"] = new Tiger(); // What stops this?
Run Code Online (Sandbox Code Playgroud)
没有致命的手可以阻止你把老虎放入IAnimals的字典中.但该字典实际上只限于包含长颈鹿.
出于同样的原因,你也不能走另一条路:
Dictionary<string, IAnimal> d3 = whatever;
d3["blake"] = new Tiger();
IDictionary<string, Giraffe> d4 = d3; // suppose this were legal
Giraffe g = d4["blake"]; // What stops this?
Run Code Online (Sandbox Code Playgroud)
现在你把老虎放在长颈鹿类型的变量中.
如果编译器可以证明不会出现这样的情况,则通用接口协方差在C#4中是合法的.
| 归档时间: |
|
| 查看次数: |
13513 次 |
| 最近记录: |