CA2227 With Dictionary有什么问题?

Dev*_*ave 3 c# dictionary fxcop asp.net-mvc-3

public Dictionary<string, string> Data { get; set; }
Run Code Online (Sandbox Code Playgroud)

使用上面的代码行我得到样式警察错误,CA2227集合属性应该是只读的.

有没有什么方法可以在不添加Stylecop抑制或必须创建自己的只读字典类的情况下移过此错误?

Bot*_*000 7

看起来你只需要删除"set"关键字.像这样的东西:

private readonly Dictionary<string, string> data = new Dictionary<string, string>();

public Dictionary<string, string> Data { get { return this.data; } }
Run Code Online (Sandbox Code Playgroud)

通常,您不需要重新分配集合,而只需清除现有集合.我猜这是警告的来源.上面的示例是我大多数时间使用的方法.