是否有一个只读的通用字典在.NET中可用?字典、有一个、NET

2023-09-02 20:41:54 作者:冰澜玉蝶

我返回引用字典,我只读属性。我如何prevent消费者改变我的数据?如果这是一个的IList 我可以简单地返回它 AsReadOnly 。是否有类似的东西我可以用一本字典吗?

 私人_mydictionary作为词典(字符串,字符串)
公共只读属性MyDictionary()作为词典(字符串,字符串)
    得到
        返回_mydictionary
    最终获取
高端物业
 

解决方案

下面是一个简单的实现,它封装了字典:

 公共类ReadOnlyDictionary< TKEY的,TValue> :IDictionary的< TKEY的,TValue>
{
    私人只读IDictionary的< TKEY的,TValue> _字典;

    公共ReadOnlyDictionary()
    {
        _dictionary =新字典< TKEY的,TValue>();
    }

    公共ReadOnlyDictionary(IDictionary的< TKEY的,TValue>字典)
    {
        _dictionary =字典;
    }

    #地区的IDictionary< TKEY的,TValue>会员

    无效的IDictionary< TKEY的,TValue>。新增(TKEY的关键,TValue值)
    {
        扔ReadOnlyException();
    }

    公共BOOL的containsKey(TKEY的键)
    {
        返回_dictionary.ContainsKey(键);
    }

    公众的ICollection< TKEY的>钥匙
    {
        {返回_dictionary.Keys; }
    }

    布尔的IDictionary< TKEY的,TValue>卸下摆臂(TKEY的键)
    {
        扔ReadOnlyException();
    }

    公共BOOL TryGetValue(TKEY的钥匙,走出TValue值)
    {
        返回_dictionary.TryGetValue(键,超时值);
    }

    公众的ICollection< TValue>值
    {
        {返回_dictionary.Values​​; }
    }

    公共TValue这[TKEY的关键]
    {
        得到
        {
            返回_dictionary [关键];
        }
    }

    TValue IDictionary的< TKEY的,TValue>。这[TKEY的关键]
    {
        得到
        {
            返回此[关键];
        }
        组
        {
            扔ReadOnlyException();
        }
    }

    #endregion

    #地区的ICollection< KeyValuePair< TKEY的,TValue>>会员

    无效的ICollection< KeyValuePair< TKEY的,TValue>>。新增(KeyValuePair< TKEY的,TValue>项目)
    {
        扔ReadOnlyException();
    }

    无效的ICollection< KeyValuePair< TKEY的,TValue>> .Clear()
    {
        扔ReadOnlyException();
    }

    公共BOOL包含(KeyValuePair< TKEY的,TValue>项目)
    {
        返回_dictionary.Contains(项目);
    }

    公共无效CopyTo从(KeyValuePair< TKEY的,TValue> []数组,诠释arrayIndex)
    {
        _dictionary.CopyTo(阵列,arrayIndex);
    }

    公众诠释计数
    {
        {返回_dictionary.Count; }
    }

    公共BOOL的IsReadOnly
    {
        获得{返回true; }
    }

    布尔的ICollection< KeyValuePair< TKEY的,TValue>>卸下摆臂(KeyValuePair< TKEY的,TValue>项目)
    {
        扔ReadOnlyException();
    }

    #endregion

    #地区的IEnumerable< KeyValuePair< TKEY的,TValue>>会员

    公众的IEnumerator< KeyValuePair< TKEY的,TValue>>的GetEnumerator()
    {
        返回_dictionary.GetEnumerator();
    }

    #endregion

    #地区IEnumerable的成员

    IEnumerator的IEnumerable.GetEnumerator()
    {
        返回的GetEnumerator();
    }

    #endregion

    私有静态异常ReadOnlyException()
    {
        返回新NotSupportedException异常(这本词典是只读);
    }
}
 
这个同步新课标的字典,更适合当下的小学语文考试

I'm returning a reference to a dictionary in my read only property. How do I prevent consumers from changing my data? If this were an IList I could simply return it AsReadOnly. Is there something similar I can do with a dictionary?

Private _mydictionary As Dictionary(Of String, String)
Public ReadOnly Property MyDictionary() As Dictionary(Of String, String)
    Get
        Return _mydictionary
    End Get
End Property

解决方案

Here's a simple implementation that wraps a dictionary :

public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
    private readonly IDictionary<TKey, TValue> _dictionary;

    public ReadOnlyDictionary()
    {
        _dictionary = new Dictionary<TKey, TValue>();
    }

    public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)
    {
        _dictionary = dictionary;
    }

    #region IDictionary<TKey,TValue> Members

    void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
    {
        throw ReadOnlyException();
    }

    public bool ContainsKey(TKey key)
    {
        return _dictionary.ContainsKey(key);
    }

    public ICollection<TKey> Keys
    {
        get { return _dictionary.Keys; }
    }

    bool IDictionary<TKey, TValue>.Remove(TKey key)
    {
        throw ReadOnlyException();
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
        return _dictionary.TryGetValue(key, out value);
    }

    public ICollection<TValue> Values
    {
        get { return _dictionary.Values; }
    }

    public TValue this[TKey key]
    {
        get
        {
            return _dictionary[key];
        }
    }

    TValue IDictionary<TKey, TValue>.this[TKey key]
    {
        get
        {
            return this[key];
        }
        set
        {
            throw ReadOnlyException();
        }
    }

    #endregion

    #region ICollection<KeyValuePair<TKey,TValue>> Members

    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
    {
        throw ReadOnlyException();
    }

    void ICollection<KeyValuePair<TKey, TValue>>.Clear()
    {
        throw ReadOnlyException();
    }

    public bool Contains(KeyValuePair<TKey, TValue> item)
    {
        return _dictionary.Contains(item);
    }

    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    {
        _dictionary.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return _dictionary.Count; }
    }

    public bool IsReadOnly
    {
        get { return true; }
    }

    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
    {
        throw ReadOnlyException();
    }

    #endregion

    #region IEnumerable<KeyValuePair<TKey,TValue>> Members

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        return _dictionary.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #endregion

    private static Exception ReadOnlyException()
    {
        return new NotSupportedException("This dictionary is read-only");
    }
}