VS2010代码分析.禁止所有类成员的消息CA1051:DoNotDeclareVisibleInstanceFields [英] VS2010 code analysis. Suppress message CA1051:DoNotDeclareVisibleInstanceFields for all class members

查看:122
本文介绍了VS2010代码分析.禁止所有类成员的消息CA1051:DoNotDeclareVisibleInstanceFields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的班级:

public class Foo
{
    public readonly int A = 1;
    public readonly int B = 2;
}

当我运行内置于Code Analysis工具中的VS2010时,会收到 2 相同的警告: " field'..."在其声明类型之外可见,将其可访问性更改为private并添加一个属性,该属性具有与该字段当前相同的可访问性,以提供对该字段的访问权限".

When I run VS2010 built in Code Analysis tool, I get 2 identical warnings: that 'field '...' is visible outside of its declaring type, change its accessibility to private and add a property, with the same accessibility as the field has currently, to provide access to it'.

我想对班级 Foo 中的所有字段禁止显示此警告,但是我不想像这样用SuppressMessage属性标记每个字段:

I want to suppress this warning for all fields in my class Foo, but I don't want to mark every field with SuppressMessage attribute like this:

public class Foo
{
    [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
    public readonly int A = 1;
    [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
    public readonly int B = 2;
}

我想使用以下代码标记所有班级成员:

I want to mark all class members, using code like this:

[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
public class Foo
{
    public readonly int A = 1;
    public readonly int B = 2;
}

但是此代码不起作用,我仍然收到代码分析警告. 我该怎么做呢?

But this code doesn't work, I still get a code analysis warning. How can I do it correctly?

推荐答案

使用SuppressMessageAttribute一次不能抑制多于一条消息.

There is no way to suppress more than 1 message at a time using SuppressMessageAttribute.

可以在此处找到讨论,但相关的部分是:

As discussion can be found here, but the relevant part is:

您对SuppressMessage遇到了常见的误解.

You are running into a common misunderstanding about SuppressMessage.

每次将SuppressMessage放入源文件中时, 恰好是一个问题(网格中只有一个行").期间.

Each time you put a SuppressMessage in a source file, you suppress exactly one problem (one "row" in the grid). Period.

可以将SuppressMessage放置在违规附近"或放置在违规位置. 模块级.模块级,程序集级和全局抑制 都意味着同一件事.通过放置在模块级别,您不必 一次抑制多个实例的问题.你只是去 将SuppressMessage放置在代码的其他位置.主要的 好处是,例如,您可以收集所有抑制因素 与单个文件中的程序集相关(例如, GlobalSuppressions.cs).

A SuppressMessage may be placed either "near" the violation or at the module-level. Module-level, assembly-level, and global suppression all mean the same thing. By placing at the module-level, you do not suppress multiple instances of the problem at once. You merely get to locate the SuppressMessage in a different place of the code. The main benefit is that you can, for example, collect all the suppressions related to the assembly in a single file (for example, GlobalSuppressions.cs).

使用模块级SuppressMessage时,必须指定 目标.目标必须与GUI中报告的内容完全匹配 违反规则.

When you use a module-level SuppressMessage, you must specify the Target. The Target must match exactly what is reported in the GUI for a violation of the rule.

无法使用SuppressMessage禁止显示规则 类的整个范围或名称空间的整个范围.

There is no way to use SuppressMessage to suppress a rule for the entire scope of a class or the entire scope of a namespace.

这篇关于VS2010代码分析.禁止所有类成员的消息CA1051:DoNotDeclareVisibleInstanceFields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆