我在只读属性中返回对字典的引用.如何防止消费者更改我的数据?如果这是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

推荐答案

下面是一个包装字典的简单实现:

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");
    }
}

.net相关问答推荐

无法在 Blazor Server 应用程序中触发 InputRadio 的 onchange 事件

正则表达式匹配 URL 中的多个子目录

C#.Net 中的可选返回

在 C# 中将字节数组保存为磁盘上的文件的最有效方法是什么?

发布版本中的 Debug.WriteLine

使用 Task.Factory.StartNew 传递方法参数

找不到 Microsoft.Office.Interop Visual Studio

如何在 WPF 应用程序中使用 App.config 文件?

如何使用转储文件来诊断内存泄漏?

找不到 Assert.Fail 和 Assert.Pass 或等效项

如何使用 Android 使用 WCF 服务

形成两个列表并集的最简单方法

C#As的 VB.NET 类似功能

MemoryCache 不遵守配置中的内存限制

是 C# 中的 bool 读/写原子

System.IO.IOException:使用 System.IO.Path.GetTempFileName() 时文件存在 - 解决方案?

.NET 中的 java.lang.IllegalStateException?

如何判断uri字符串是否有效

实体框架太慢了.我有哪些 Select ?

检测到包降级警告(dotnet core,vs 2017)