FxCop CA2227 警告和 ReadOnlyCollection

发布于 2024-08-31 01:06:36 字数 689 浏览 3 评论 0 原文

在我的 VS2008 SP1、.NET 3.5 SP1 项目中,我有包含不同属性的不同类。 我经常使用 C#3.0 自动属性。

其中一些属性需要是集合。由于我想让事情变得简单,因此我对这些属性使用 ReadOnlyCollection

我不想使用 IEnumerable 因为我想要随机访问元素。

我使用代码分析(FxCop 规则)并收到 CA2227 警告。

我不明白为什么 ReadOnlyCollection 应该有一个 set 方法,而它不能更改...... set 方法只能执行属性可以执行的操作。

示例:

using System.Collections.ObjectModel;

namespace CA2227
{
    public class MyClass
    {
        public ReadOnlyCollection<int> SomeNumbers { get; set; }
    }
}

CA2227:Microsoft.Usage:通过删除属性设置器将“MyClass.SomeNumbers”更改为只读。 C:\用户...\Visual Studio 2008\项目\CA2227\MyClass.cs 7 CA2227

In my VS2008 SP1, .NET 3.5 SP1 project, I have different classes that contain different properties.
I use C#3.0 auto properties a lot.

Some of these properties need to be collections. Since I want to make it simple, I use ReadOnlyCollection<T> for these properties.

I don't want to use IEnumerable<T> since I want random access to the elements.

I use Code Analysis (FxCop rules) and I get the CA2227 warning.

I don't understand why does ReadOnlyCollection<T> should have a set method while it can't be changed... The set method can only do exactly what the property can do.

Example:

using System.Collections.ObjectModel;

namespace CA2227
{
    public class MyClass
    {
        public ReadOnlyCollection<int> SomeNumbers { get; set; }
    }
}

CA2227 : Microsoft.Usage : Change 'MyClass.SomeNumbers' to be read-only by removing the property setter. C:\Users...\Visual Studio 2008\Projects\CA2227\MyClass.cs 7 CA2227

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℉服软 2024-09-07 01:06:37

阻止对集合内容的更改而不阻止对集合本身的更改是相当奇怪的。如果您希望能够在类中设置集合,同时保留自动属性的使用,则可以使用私有设置器。例如:

public ReadOnlyCollection<int> SomeNumbers { get; private set; }

It's rather odd to block changes to the contents of the collection without also blocking changes to the collection itself. If you want to be able to set the collection from within your class while conserving the use of automatic properties, you could use a private setter. e.g.:

public ReadOnlyCollection<int> SomeNumbers { get; private set; }
千里故人稀 2024-09-07 01:06:37

考虑使用

public class MyClass
{
    public IReadOnlyList<int> SomeNumbers { get; set; }
}

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

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

ReadOnlyCollection 的问题在于它仍然继承自 ICollection,并且仍然具有 .Add,即使文档说它会抛出 - http://msdn.microsoft.com/en-us/library/cc672239(v=vs.110).aspx

Consider using

public class MyClass
{
    public IReadOnlyList<int> SomeNumbers { get; set; }
}

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

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

The problem with ReadOnlyCollection, is that it still inherits from ICollection, and still has .Add, even though documentation say it will throw - http://msdn.microsoft.com/en-us/library/cc672239(v=vs.110).aspx

无所谓啦 2024-09-07 01:06:36

ReadOnlyCollection 无法更改,但没有理由不能更改具有 ReadOnlyCollection 类型的 setter 的属性以引用不同的 ReadOnlyCollection< /代码>。如果您希望 SomeNumbers 属性是不可变的,那么它必须是只读类型,并且还需要有一个非公共 setter。

编辑

如果您确信自己想要什么,那么尽管 FxCop 警告您是正确的,但您对警告感到满意。如果您想摆脱它,请在此时包含一个 SuppressMessage 属性 - 只要您在构建之前还在项目属性中定义了 CODE_ANALYSIS 常量,FxCop 将尊重该属性,而不是在特定场合发出特定警告。

A ReadOnlyCollection cannot be changed, but there's no reason why a property with a setter that is of type ReadOnlyCollection can't be changed to refer to a different ReadOnlyCollection. If you want the SomeNumbers property to be immutable, then it needs to be both of a read-only type, and also have a non-public setter.

EDIT

If you're convinced in what you want, then although FxCop is correct to warn you, you are happy with the warning. If you want to get rid of it, then include a SuppressMessage attribute at that point - as long as you also define a CODE_ANALYSIS constant in the project properties before you build, FxCop will honour that attribute and just not issue that particular warning on that particular occasion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文