CA2104警告:有什么方法可以将某个类标记为“不可变”以禁止它? [英] The CA2104 warning: Is there any way to mark a class as `Immutable` to suppress it?
问题描述
请考虑以下代码,该代码会引发 CA2104:请勿声明只读可变引用类型。
公共类Test
{
// //引起CA2104:不要声明只读可变引用类型。
受保护的只读ImmutableClass ImmutableMember;
}
公共类ImmutableClass
{
}
有人知道不会以警告CA2104的方式将类标记为不可变吗?
我尝试装饰
> [ImmutableObject(true)]
,但没有成功的希望(因为该属性很明显可供表单编辑器使用),并且肯定它不起作用。
我假设代码分析在确定是否发出CA2104时使用的是已知的不可变类型的列表,因此我们不能使用相同的方法。
我想即使您可以将一个类标记为不可变,编译器也无法实际检查它是否为真,但至少可以
无论如何,我有没有忽略任何属性?
目前看来,没有其他方法可以执行此操作。
我确实找到了一个乔·达菲( Windows上并行编程的作者)的一个有趣的博客,关于这种事情。
他从开始有一个ImmutableAttribute。 ...:)
这很有趣-他费心编写一些新的FxCop规则来进行类型分析
受保护的只读
字段不是真正的很清楚。由于受保护的
,您可能希望派生类可以初始化该字段:
公共类测试
{
受保护的只读ImmutableClass ImmutableMember;
}
公共类SpecialTest:测试
{
public SpecialTest(){ImmutableMember = new ImmutableClass; }
}
但是,事实并非如此,您会得到编译错误(CS0191)。 / p>
我不知道CA2104的确切推动力,但是您可以通过以下方式获得相同的结果而无需只读:
public class Test
{
protected ImmutableClass ImmutableMember {get;私有集;}
public Test()
:this(new ImmutableClasse())
{
}
public Test(ImmutableClass immutableClass)
{
ImmutableMember = new ImmutableClasse();
}
}
,并避免使用CA2104。
更新:
wrt就像您说的那样(对于将来的读者),您可以使用后备字段获取只读并提供受保护的getter来在派生类中获取它:
公共类Test
{
private readonly ImmutableClass immutableMember;
受保护的ImmutableClass ImmutableMember {获取{return immutableMember; }}
公共测试(ImmutableClass immutableMember)
{
this.immutableMember = immutableMember;
}
}
Consider the following code, which provokes CA2104: Do not declare read only mutable reference types.
public class Test
{
// This provokes CA2104: "Do not declare read only mutable reference types".
protected readonly ImmutableClass ImmutableMember;
}
public class ImmutableClass
{
}
Does anyone know of a way to mark a class as immutable in a way that would suppress warning CA2104?
I tried decorating MutableClass
with [ImmutableObject(true)]
with no hope of success (since that attribute is pretty clearly for the Form Editor to use), and sure enough it doesn't work.
I assume that Code Analysis is using a list of known immutable types when determining whether to emit CA2104, so we can't use the same approach.
I guess that even if you could mark a class as immutable, there'd be no way for the compiler to actually check if it was true, but at least it could be a useful indicator.
Anyway, are there any attributes I'm overlooking? If not, suppression will have to do.
It seems that there is no alternative way to do this at the moment.
I did find an interesting blog from Joe Duffy (author of "Concurrent Programming On Windows") about this kind of thing.
He starts off with "Imagine we had an ImmutableAttribute."... :)
It's quite interesting - he went to the trouble of writing some new FxCop rules to do some analysis of types attributed as immutable.
A protected readonly
field isn't really that clear. As protected
, you might expect that a derived class can initialize the field:
public class Test
{
protected readonly ImmutableClass ImmutableMember;
}
public class SpecialTest : Test
{
public SpecialTest() { ImmutableMember = new ImmutableClass; }
}
But, that's not the case--you will get a compile error (CS0191).
I don't know the exact impetus behind CA2104, but you can get the same result without readonly via:
public class Test
{
protected ImmutableClass ImmutableMember {get; private set;}
public Test()
:this(new ImmutableClasse())
{
}
public Test(ImmutableClass immutableClass)
{
ImmutableMember = new ImmutableClasse();
}
}
and avoid the CA2104.
Update:
w.r.t. to the comments (and to future readers), as you say you could use a backing field to get the read-only and provide a protected getter to get at it in derived classes:
public class Test
{
private readonly ImmutableClass immutableMember;
protected ImmutableClass ImmutableMember { get { return immutableMember; } }
public Test(ImmutableClass immutableMember)
{
this.immutableMember = immutableMember;
}
}
这篇关于CA2104警告:有什么方法可以将某个类标记为“不可变”以禁止它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!