如何将 CA [SuppressMessage] 标记为“有福”?
尽管我尽可能将代码库中的 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使属性没有被密封,子类化方法也不起作用,因为代码分析引擎会筛选精确的 SuppressMessageAttribute 实例。在编写当前版本时,引擎将完全忽略子类实例。
就我个人而言,我使用以下方法来管理抑制:
有一个 Justification 属性
解释了原因
抑制。
抑制措施必须放在
目标代码文件。否则,他们
必须放置在顶部
全局抑制文件。
放置在“临时
抑制”标题注释中
GlobalSuppressions 文件,它们
不应被分配
理由。
需要接受代码审查,准确地说
与代码本身相同的方式。
我使用差异检测来标记
代码供审查,这样我就可以看到哪个
已添加抑制或
正如我所看到的那样容易改变
哪些代码已被修改。
如果这种事情对您不起作用,另一种方法是使用独立的 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:
have a Justification property that
explains the reason for the
suppression.
suppressions must be placed in the
target code file. Otherwise, they
must be placed at the top of the
GlobalSuppressions file.
placed below a "Temporary
suppressions" header comment in the
GlobalSuppressions file, and they
should not be assigned a
justification.
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.
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.
当您右键单击错误列表中的 CA 警告时,给出的选项之一是抑制消息 ->在项目抑制文件中。当您选择该选项时,Visual Studio 会将如下行添加到项目中的 GlobalSuppressions.cs 文件中:
这使它们分组在一起并且不存在于您的代码文件中。这就是你想要达到的目的吗?
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:
This keeps them grouped together and out of your code files. Is this what you are trying to get at?