【发布时间】:2010-02-16 16:25:38
【问题描述】:
我一直试图解决这个 FXCop 违规“DoNotDeclareReadOnlyMutableReferenceTypes”
MSDN:http://msdn.microsoft.com/en-us/library/ms182302%28VS.80%29.aspx
来自 MSDN 的可能导致此违规的代码:
namespace SecurityLibrary
{
public class MutableReferenceTypes
{
static protected readonly StringBuilder SomeStringBuilder;
static MutableReferenceTypes()
{
SomeStringBuilder = new StringBuilder();
}
}
}
从 Jon 的回答 here 和 here 中,我了解到保存对象引用的字段(在本例中为 SomeStringBuilder)是只读的,而不是对象本身(由 new StringBuilder() 创建)
以这个例子为例,一旦字段引用了对象,我将如何更改对象本身?我喜欢 Eric Lippert's example 如何更改只读数组,并希望看到任何其他可变引用类型的类似内容
【问题讨论】:
标签: c# string fxcop immutability readonly