防止在 FxCop 中多次报告同一规则违规行为 - 什么是 Id?

发布于 2024-08-26 20:50:41 字数 389 浏览 3 评论 0原文

FxCop 当前正在报告特定方法的相同规则违规 - 它有两个输出参数,因为我想向调用者返回两个值而不为其创建结构。我想知道匿名类型是否可以解决我的问题,但在编写该方法时我并不知道它们。

无论如何,我为每个参数报告了一次 CheckId CA1021。我从 FxCop 复制了 SuppressMessage 文本,然后意识到每条消息的 Id 都是不同的!对我来说,似乎你只需要 CheckId,那么......

  1. Id 是用来做什么的?我在网上找不到有关它的信息。
  2. Id 会保持不变吗?我认为是这样,否则 SuppressMessage 不会按照人们希望的方式工作
  3. 是否有一种方法可以指定 SuppressMessage 属性,以便它抑制所有 Id?

FxCop is currently reporting the same rule violation for a particular method -- it has two out parameters, because I want to return two values to the caller without creating a struct for it. I wonder if anonymous types would solve my problem, but I didn't know about them at the time I had written the method.

Anyhow, I'm getting CheckId CA1021 reported once for each parameter. I've copied the SuppressMessage text from FxCop, and then realized that the Id for each message is different! To me, it seems like you only need the CheckId, so...

  1. what is the Id used for? I haven't been able to find information about it online.
  2. will the Id remain the same? I assume so, or SuppressMessage wouldn't work the way one would want it to
  3. is there a way to specify the SuppressMessage attribute so that it suppresses for all Ids?

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

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

发布评论

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

评论(1

晚雾 2024-09-02 20:50:41

来自源代码抑制概述

属性只能应用于方法,不能嵌入方法体内。但是,您可以将标识符指定为消息 ID,以区分方法内多次发生的违规。

换句话说: MessageId 用于抑制范围内的单个错误实例;如果您担心在同一方法中意外地再次引入错误并希望使用 FxCop 捕获新的引入(或者如果您想为每次出现问题指定不同的理由),那么这会很方便。如果您不怕引入更多此类错误(或者不在乎是否会引入),并且有同样的理由,则可以简单地在 Suppress 语句中省略 MessageId:[SuppressMessage("Microsoft.设计”,“CA1021:避免OutParameters”)]

From In Source Suppression Overview

Attributes can only be applied to a method and cannot be embedded within the method body. However, you can specify the identifier as the message ID to distinguish multiple occurrences of a violation within a method.

In other words: The MessageId is used to suppress a single instance of the error within the scope; this is handy if you're worried about accidentally introducing the error again within the same method and want to catch that new introduction with FxCop (or if you want to specify different justifications for each occurrence of the issue). If you aren't afraid of introducing more of these errors (or don't care if you do), and have the same justification, you can simply leave off the MessageId in the Suppress statement: [SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters")].

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