排除源代码中的代码分析规则

发布于 2024-07-04 17:55:47 字数 605 浏览 8 评论 0原文

在我正在开发的 FxCop 项目中,我看到 Windows 窗体设计器生成的 InitializeComponent() 方法上有很多(我的意思是超过 400 个)错误。 大多数这些错误只是标签的 Text 属性的分配。

我想在源代码中抑制这些方法,因此我将 FxCop 生成的抑制代码复制到 AssemblyInfo.cs 中,但它不起作用。

这是 FxCop 复制到剪贴板的属性。

[module: SuppressMessage("Microsoft.Globalization",
    "CA1303:DoNotPassLiteralsAsLocalizedParameters",
    Scope = "member",
    Target = "WindowsClient.MainForm.InitializeComponent():System.Void",
    MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]

有人知道抑制此消息的正确属性吗?

PS:我使用的是 Visual Studio 2005、C#、FxCop 1.36 beta。

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.

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.

This is the attribute that FxCop copied to the clipboard.

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

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

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

发布评论

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

评论(3

夏九 2024-07-11 17:55:47

您可能已经获得了正确的代码,但您还需要在项目属性中添加 CODE_ANALYSIS 作为预编译器定义的符号。 我认为如果定义了 CODE_ANALYSIS,这些 SuppressMessage 属性只会保留在编译的二进制文件中。

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.

此生挚爱伱 2024-07-11 17:55:47

在 FxCop 1.36 中,“拼写和分析”选项卡上实际上有一个项目选项,可以禁止对任何生成的代码进行分析。

如果您不想关闭所有生成代码的分析,则需要确保将 CODE_ANALYSIS 符号添加到条件编译符号列表(项目属性,“生成”选项卡)。 如果没有定义此符号,SupressMessage 属性将从编译的代码中删除,因此 FxCop 将看不到它们。

SuppressMessage 属性的另一个问题是您列出了特定方法名称的“目标”(在本例中为 WindowsClient.MainForm.InitializeComponent():System.Void)并列出了特定的“范围”。 您可能想尝试删除这些; 否则,您应该将此 SuppressMessage 添加到该方法的每个实例中。

您还应该升级到 RTM 版本从 FxCop 1.36 开始,测试版将不会自动检测较新的版本。

In FxCop 1.36 there is actually a project option on the "Spelling & Analysis" tab that will supress analysis for any generated code.

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.

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.

You should also upgrade to the RTM version of FxCop 1.36, the beta will not automatically detect the newer version.

酷炫老祖宗 2024-07-11 17:55:47

模块级抑制消息需要粘贴到与命名空间声明之前或 assemblyinfo.cs 中引发 FxCop 错误的代码相同的文件中。 此外,您需要将 CODE_ANALYSIS 定义为条件编译器符号(项目 > 属性 > 构建)。 一旦到位,对项目进行完整的重建,下次运行 FxCop 时,错误应移至“源中排除”选项卡。

另外,还有一个小技巧,但如果您正在处理大量 FxCop 排除项,那么在它们周围包裹一个区域可能会很有用,这样您就可以将它们排除在外。

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.

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.

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