微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 无法将.Net ConcurrentDictionary转换为IReadOnlyDictionary接口?

我必须遗漏一些东西,觉得我必须回到基础,但是.Net 4.5中的ConcurrentDictionary according to the reference sources它实现了IReadOnlyDictionary接口(虽然有些成员明确地说),但由于某种原因我无法将实例强制转换为该接口 – 这是为什么?
IDictionary<int,string> a = new ConcurrentDictionary<int,string>(); // works
IReadOnlyDictionary<int,string> b = new ConcurrentDictionary<int,string>(); // does not work

.. 这是为什么?

为了使这一点更清楚:

解决方法

您可能使用.NET版本,其中此接口未由 ConcurrentDictionary实现.从我尝试过的版本不是由4.6之前的.NET版本实现的:
[SerializableAttribute]
[ComVisibleAttribute(false)]
[HostProtectionAttribute(SecurityAction.LinkDemand,Synchronization = true,ExternalThreading = true)]
public class ConcurrentDictionary<TKey,TValue> : IDictionary<TKey,TValue>,ICollection<keyvaluePair<TKey,TValue>>,IEnumerable<keyvaluePair<TKey,IDictionary,ICollection,IEnumerable

在当前的.NET框架版本(4.6.2)中,ConcurrentDictionary实现了它:

[SerializableAttribute]
[ComVisibleAttribute(false)]
[HostProtectionAttribute(SecurityAction.LinkDemand, TValue>, TValue>>,IEnumerable,IReadOnlyDictionary<TKey,IReadOnlyCollection<keyvaluePair<TKey, TValue>>

原文地址:https://www.jb51.cc/csharp/98411.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐