visual-studio 在源中排除代码分析规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excluding Code Analysis rule in source
提问by Julio César
In a project I'm working on FxCop shows me lots of (and I mean more than 400) errors on the InitializeComponent() methods generated by the Windows Forms designer. Most of those errors are just the assignment of the Text property of labels.
在我处理 FxCop 的一个项目中,Windows 窗体设计器生成的 InitializeComponent() 方法上有很多(我的意思是 400 多个)错误。大多数这些错误只是标签的 Text 属性的分配。
I'd like to suppress those methods in source, so I copied the suppression code generated by FxCop into AssemblyInfo.cs, but it doesn't work.
我想在源代码中抑制这些方法,所以我将 FxCop 生成的抑制代码复制到 AssemblyInfo.cs 中,但它不起作用。
This is the attribute that FxCop copied to the clipboard.
这是 FxCop 复制到剪贴板的属性。
[module: SuppressMessage("Microsoft.Globalization",
"CA1303:DoNotPassLiteralsAsLocalizedParameters",
Scope = "member",
Target = "WindowsClient.MainForm.InitializeComponent():System.Void",
MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]
Anyone knows the correct attribute to suppress this messages?
任何人都知道抑制此消息的正确属性?
PS: I'm using Visual Studio 2005, C#, FxCop 1.36 beta.
PS:我使用的是 Visual Studio 2005、C#、FxCop 1.36 测试版。
采纳答案by Matt Hamilton
You've probably got the right code, but you also need to add CODE_ANALYSIS as a precompiler defined symbol in the project properties. I think those SuppressMessage attributes are only left in the compiled binaries if CODE_ANALYSIS is defined.
您可能已经获得了正确的代码,但您还需要在项目属性中添加 CODE_ANALYSIS 作为预编译器定义的符号。我认为如果 CODE_ANALYSIS 被定义,那些 SuppressMessage 属性只会留在编译的二进制文件中。
回答by Scott Dorman
In FxCop 1.36 there is actually a project option on the "Spelling & Analysis" tab that will supress analysis for any generated code.
在 FxCop 1.36 中,“拼写和分析”选项卡上实际上有一个项目选项,可以禁止对任何生成的代码进行分析。
If you don't want to turn analysis off for all generated code, you need to make sure that you add a CODE_ANALYSIS symbol to the list of conditional compilation symbols (project properties, Build tab). Without this symbol defined, the SupressMessage attributes will be removed from the compiled code so FxCop won't see them.
如果不想对所有生成的代码关闭分析,则需要确保将 CODE_ANALYSIS 符号添加到条件编译符号列表(项目属性、构建选项卡)。如果没有定义此符号,SupressMessage 属性将从编译代码中删除,因此 FxCop 将看不到它们。
The other problem with your SuppressMessage attribute is that you are listing a "Target" of a specific method name (in this case WindowsClient.MainForm.InitializeComponent():System.Void) and listing a specific "Scope". You may want to try removing these; otherwise you should add this SuppressMessage to each instance of the method.
您的 SuppressMessage 属性的另一个问题是您列出了特定方法名称的“目标”(在本例中为 WindowsClient.MainForm.InitializeComponent():System.Void)并列出了特定的“范围”。您可能想尝试删除这些;否则,您应该将此 SuppressMessage 添加到该方法的每个实例中。
You should also upgrade to the RTM versionof FxCop 1.36, the beta will not automatically detect the newer version.
您还应该升级到FxCop 1.36的RTM 版本,测试版不会自动检测到较新的版本。
回答by rjzii
Module level suppression messages need to be pasted into the same file as the code that is raising the FxCop error before the namespace declaration or in assemblyinfo.cs. Additionally, you will need to have CODE_ANALYSIS defined as a conditional compiler symbols (Project > Properties > Build). Once that is in place, do a complete rebuild of project and the next time you run FxCop the error should be moved to the "Excluded in Source" tab.
模块级抑制消息需要粘贴到与在命名空间声明之前或 assemblyinfo.cs 中引发 FxCop 错误的代码相同的文件中。此外,您需要将 CODE_ANALYSIS 定义为条件编译器符号(项目 > 属性 > 构建)。一旦到位,对项目进行完整的重建,下次运行 FxCop 时,错误应移至“在源中排除”选项卡。
Also, one small tip, but if you are dealing with a lot of FxCop exclusions it might be useful to wrap a region around them so you can get them out of the way.
此外,还有一个小技巧,但如果您正在处理大量 FxCop 排除项,那么在它们周围环绕一个区域可能会很有用,这样您就可以将它们排除在外。

