如何将 CA [SuppressMessage] 标记为“有福”?

发布于 2024-09-25 08:14:30 字数 358 浏览 6 评论 0原文

尽管我尽可能将代码库中的 [SuppressMessage] 保持在最低限度,但仍不断引入新的内容。我想在代码中用某种方式说“我已经审查了这个[SuppressMessage]并且它是‘可接受的’”。

一种想法是创建我自己的 My.CodeAnalysis.SuppressMessageAttribute 类,该类继承自 System.Diagnostics.CodeAnalysis.SuppressMessageAttribute,但该类是密封的。

我确信我可以通过属性附近的注释和后处理步骤拼凑出一些东西,但我想坚持使用 Visual Studio 2010 中“现成的”可用的内容。

Try as I might to keep [SuppressMessage]s to a minimum in the codebase, new ones continue to get introduced. I would like some way to say in the code "I've reviewed this [SuppressMessage] and it is 'acceptable'".

One thought was to create my own My.CodeAnalysis.SuppressMessageAttribute class which inherits from System.Diagnostics.CodeAnalysis.SuppressMessageAttribute, but that class is sealed.

I'm sure I could cobble together something with a comment near the attribute and a post-processing step, but I'd like to stick to what is available "in the box" with Visual Studio 2010.

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

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

发布评论

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

评论(2

撩起发的微风 2024-10-02 08:14:30

即使属性没有被密封,子类化方法也不起作用,因为代码分析引擎会筛选精确的 SuppressMessageAttribute 实例。在编写当前版本时,引擎将完全忽略子类实例。

就我个人而言,我使用以下方法来管理抑制:

  1. 所有“永久”抑制必须
    有一个 Justification 属性
    解释了原因
    抑制。
  2. 如有可能,永久
    抑制措施必须放在
    目标代码文件。否则,他们
    必须放置在顶部
    全局抑制文件。
  3. 暂时的压制必须全部
    放置在“临时
    抑制”标题注释中
    GlobalSuppressions 文件,它们
    不应被分配
    理由。
  4. 所有永久压制都是
    需要接受代码审查,准确地说
    与代码本身相同的方式。
    我使用差异检测来标记
    代码供审查,这样我就可以看到哪个
    已添加抑制或
    正如我所看到的那样容易改变
    哪些代码已被修改。
  5. 临时抑制必须在项目的预定义阶段全部消除。此点过后,将启用检测缺少对齐属性的规则。

如果这种事情对您不起作用,另一种方法是使用独立的 FxCop 来注释您抑制的违规行为。不幸的是,这些注释将驻留在您的代码库之外,但如果您想防止开发人员“祝福”自己的抑制,那么这可能更适合您。

Even if the attribute weren't sealed, a subclassing approach would not work since the code analysis engine screens for exact SuppressMessageAttribute instances. As current versions are written, the engine would completely ignore subclass instances.

Personally, I use the following approach for management of suppressions:

  1. All "permanent" suppressions must
    have a Justification property that
    explains the reason for the
    suppression.
  2. Where possible, permanent
    suppressions must be placed in the
    target code file. Otherwise, they
    must be placed at the top of the
    GlobalSuppressions file.
  3. Temporary suppressions must all be
    placed below a "Temporary
    suppressions" header comment in the
    GlobalSuppressions file, and they
    should not be assigned a
    justification.
  4. All permanent suppressions are
    subject to code review, in exactly
    the same manner as the code itself.
    I use difference detection to flag
    code for review, so I can see which
    suppressions have been added or
    changed just as easily as I can see
    which code has been modified.
  5. Temporary suppressions must all be removed by a pre-defined phase of the project. After this point has passed, a rule that detects missing Justification properties is enabled.

If this sort of thing wouldn't work for you, another approach would be to use stand-alone FxCop to annotate your suppressed violations. Unfortunately, these annotations would reside outside your code base, but maybe that would suit you better if you want to prevent developers from "blessing" their own suppressions.

娇俏 2024-10-02 08:14:30

当您右键单击错误列表中的 CA 警告时,给出的选项之一是抑制消息 ->在项目抑制文件中。当您选择该选项时,Visual Studio 会将如下行添加到项目中的 GlobalSuppressions.cs 文件中:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Autofac")]

这使它们分组在一起并且不存在于您的代码文件中。这就是你想要达到的目的吗?

When you right-click on the CA warning in the errors list, one of the options given is Suppress Message(s) -> In Project Suppression File. When you select that, Visual Studio will add a line like the following to a GlobalSuppressions.cs file in your project:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Autofac")]

This keeps them grouped together and out of your code files. Is this what you are trying to get at?

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