gpt4 book ai didi

visual-studio - VS2010代码分析。抑制消息 CA1051 :DoNotDeclareVisibleInstanceFields for all class members

转载 作者:行者123 更新时间:2023-12-04 17:12:17 25 4
gpt4 key购买 nike

我有一堂像这样的课:

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

当我运行内置代码分析工具的 VS2010 时,我得到 2 相同的警告:
'字段'...'在其声明类型之外可见,将其可访问性更改为私有(private)并添加一个与该字段当前具有相同可访问性的属性以提供对其的访问权限。

我想对我类(class)中的所有字段禁用此警告 ,但我不想像这样用 SuppressMessage 属性标记每个字段:
public class Foo
{
[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
public readonly int A = 1;
[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
public readonly int B = 2;
}

我想使用如下代码标记所有类(class)成员:
[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
public class Foo
{
public readonly int A = 1;
public readonly int B = 2;
}

但是这段代码不起作用,我仍然收到代码分析警告。
我怎样才能正确地做到这一点?

最佳答案

无法使用 SuppressMessageAttribute 一次抑制超过 1 条消息。

可以找到讨论 here ,但相关部分是:

You are running into a common misunderstanding about SuppressMessage.

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

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).

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.

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.

关于visual-studio - VS2010代码分析。抑制消息 CA1051 :DoNotDeclareVisibleInstanceFields for all class members,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7092778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com