.NET中是否有只读通用字典?

我在我的只读属性中返回对字典的引用。如何防止消费者更改我的数据?如果这是一个IList我可以简单地返回它AsReadOnly。我能用字典做些什么吗?


Private _mydictionary As Dictionary(Of String, String)

Public ReadOnly Property MyDictionary() As Dictionary(Of String, String)

    Get

        Return _mydictionary

    End Get

End Property


鸿蒙传说
浏览 501回答 3
3回答

慕斯709654

这是一个包装字典的简单实现:public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>{&nbsp; &nbsp; private readonly IDictionary<TKey, TValue> _dictionary;&nbsp; &nbsp; public ReadOnlyDictionary()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _dictionary = new Dictionary<TKey, TValue>();&nbsp; &nbsp; }&nbsp; &nbsp; public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _dictionary = dictionary;&nbsp; &nbsp; }&nbsp; &nbsp; #region IDictionary<TKey,TValue> Members&nbsp; &nbsp; void IDictionary<TKey, TValue>.Add(TKey key, TValue value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw ReadOnlyException();&nbsp; &nbsp; }&nbsp; &nbsp; public bool ContainsKey(TKey key)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _dictionary.ContainsKey(key);&nbsp; &nbsp; }&nbsp; &nbsp; public ICollection<TKey> Keys&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _dictionary.Keys; }&nbsp; &nbsp; }&nbsp; &nbsp; bool IDictionary<TKey, TValue>.Remove(TKey key)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw ReadOnlyException();&nbsp; &nbsp; }&nbsp; &nbsp; public bool TryGetValue(TKey key, out TValue value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _dictionary.TryGetValue(key, out value);&nbsp; &nbsp; }&nbsp; &nbsp; public ICollection<TValue> Values&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _dictionary.Values; }&nbsp; &nbsp; }&nbsp; &nbsp; public TValue this[TKey key]&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _dictionary[key];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; TValue IDictionary<TKey, TValue>.this[TKey key]&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this[key];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw ReadOnlyException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; #endregion&nbsp; &nbsp; #region ICollection<KeyValuePair<TKey,TValue>> Members&nbsp; &nbsp; void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw ReadOnlyException();&nbsp; &nbsp; }&nbsp; &nbsp; void ICollection<KeyValuePair<TKey, TValue>>.Clear()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw ReadOnlyException();&nbsp; &nbsp; }&nbsp; &nbsp; public bool Contains(KeyValuePair<TKey, TValue> item)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _dictionary.Contains(item);&nbsp; &nbsp; }&nbsp; &nbsp; public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _dictionary.CopyTo(array, arrayIndex);&nbsp; &nbsp; }&nbsp; &nbsp; public int Count&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _dictionary.Count; }&nbsp; &nbsp; }&nbsp; &nbsp; public bool IsReadOnly&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return true; }&nbsp; &nbsp; }&nbsp; &nbsp; bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw ReadOnlyException();&nbsp; &nbsp; }&nbsp; &nbsp; #endregion&nbsp; &nbsp; #region IEnumerable<KeyValuePair<TKey,TValue>> Members&nbsp; &nbsp; public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _dictionary.GetEnumerator();&nbsp; &nbsp; }&nbsp; &nbsp; #endregion&nbsp; &nbsp; #region IEnumerable Members&nbsp; &nbsp; IEnumerator IEnumerable.GetEnumerator()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return GetEnumerator();&nbsp; &nbsp; }&nbsp; &nbsp; #endregion&nbsp; &nbsp; private static Exception ReadOnlyException()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return new NotSupportedException("This dictionary is read-only");&nbsp; &nbsp; }}

MMTTMM

.NET 4.5.NET Framework 4.5 BCL介绍ReadOnlyDictionary<TKey, TValue>(源代码)。由于.NET Framework 4.5 BCL不包含AsReadOnlyfor词典,因此您需要编写自己的(如果需要)。它将类似于以下内容,其简单性可能突出了它为什么不是.NET 4.5的优先级。public static ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(&nbsp; &nbsp; this IDictionary<TKey, TValue> dictionary){&nbsp; &nbsp; return new ReadOnlyDictionary<TKey, TValue>(dictionary);}.NET 4.0及更低版本在.NET 4.5之前,没有.NET框架类包装Dictionary<TKey, TValue>类似于ReadOnlyCollection包装List。但是,创建一个并不困难。这是一个例子 - 如果你是谷歌的ReadOnlyDictionary,还有很多其他的。

海绵宝宝撒

在最近的BUILD会议上宣布,自.NET 4.5以来,System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>包含了界面。证明在这里(单声道)和这里(微软);)不确定是否ReadOnlyDictionary也包括在内,但至少在界面上创建一个暴露官方.NET通用接口的实现应该不难。
打开App,查看更多内容
随时随地看视频慕课网APP