9

我在 C# 中有一个包含字典的类,只要包含它的类存在,我想创建它并确保从该字典中添加、编辑或删除任何内容。

readonly 并没有真正帮助,一旦我测试并看到我可以在之后添加项目。例如,我创建了一个示例:

public class DictContainer
{
    private readonly Dictionary<int, int> myDictionary;

    public DictContainer()
    {
        myDictionary = GetDictionary();
    }

    private Dictionary<int, int> GetDictionary()
    {
        Dictionary<int, int> myDictionary = new Dictionary<int, int>();

        myDictionary.Add(1, 2);
        myDictionary.Add(2, 4);
        myDictionary.Add(3, 6);

        return myDictionary;
    }

    public void Add(int key, int value)
    {
        myDictionary.Add(key, value);
    }

}

我希望 Add 方法不起作用。如果可能的话,我希望它甚至不编译。有什么建议么?

实际上,我很担心它是开放给很多人更改的代码。因此,即使我隐藏了 Add 方法,也有可能有人“无辜地”创建一个添加密钥或删除另一个密钥的方法。我希望人们查看并知道他们不应该以任何方式更改字典。就像我使用 const 变量一样。

4

7 回答 7

8

完全隐藏字典。只需在 DictContainer 类上提供一个从字典中检索项目的 get 方法。

public class DictContainer
{
    private readonly Dictionary<int, int> myDictionary;

    public DictContainer()
    {
        myDictionary = GetDictionary();
    }

    private Dictionary<int, int> GetDictionary()
    {
        Dictionary<int, int> myDictionary = new Dictionary<int, int>();

        myDictionary.Add(1, 2);
        myDictionary.Add(2, 4);
        myDictionary.Add(3, 6);

        return myDictionary;
    }

    public this[int key]
    {
        return myDictionary[key];
    }
}
于 2009-05-12T16:56:09.107 回答
2

不要定义Add方法。

保持myDictionary变量私有并公开一个 Getter/Indexer,以便只能从该类外部读取它。

于 2009-05-12T16:56:05.723 回答
1

没有内置的方法可以做到这一点,请考虑使用包装类。

于 2009-05-12T16:56:48.000 回答
0
interface IReadOnlyDic<Key, Value>
{
    void Add(Key key, Value value);
}
class ReadOnlyDic<Key, Value> : Dictionary<Key, Value>, IReadOnlyDic<Key, Value>
{
    public new void Add(Key key, Value value)
    {
        //throw an exception or do nothing
    }
    #region IReadOnlyDic<Key,Value> Members

    void IReadOnlyDic<Key, Value>.Add(Key key, Value value)
    {
        base.Add(key, value);
    }

    #endregion
}

添加自定义项目;

    IReadOnlyDic<int, int> dict = myDictInstance as IReadOnlyDic<int, int>;
    if (dict != null)
        dict.Add(1, 155);
于 2009-05-12T17:17:00.400 回答
0

这是另一种方式

class ReadOnlyDic<Key, Value> : Dictionary<Key, Value>
{
    private bool _locked = false;

    public new void Add(Key key, Value value)
    {
        if (!_locked)
        {
            base.Add(key, value);
        }
        else
        {
            throw new ReadOnlyException();
        }
    }
    public void Lock()
    {
        _locked = true;
    }
}
于 2009-05-12T17:21:07.617 回答
0

仅供参考,现在它内置于 .net 4.5。

http://msdn.microsoft.com/en-us/library/gg712875(v=vs.110).aspx

于 2014-11-11T21:52:18.187 回答
0

类似于尼尔的回答

完全隐藏字典。只需在 DictContainer 类上提供一个从字典中检索项目的 get 方法。如果你想使用 [] 覆盖你需要 getter settter 方法(至少任何一个 get/set)

public class DictContainer
{
    private readonly Dictionary<int, int> myDictionary;

    public DictContainer()
    {
        myDictionary = GetDictionary();
    }

    private Dictionary<int, int> GetDictionary()
    {
        Dictionary<int, int> myDictionary = new Dictionary<int, int>();

        myDictionary.Add(1, 2);
        myDictionary.Add(2, 4);
        myDictionary.Add(3, 6);

        return myDictionary;
    }

    public this[int key]
    {
        get => myDictionary[key];
    }
}
于 2021-12-01T15:10:04.577 回答