抑制生成的 C# 代码的警告

发布于 2024-08-30 01:41:43 字数 222 浏览 5 评论 0原文

我已经为我的 VS 项目打开了“将警告视为错误”,这意味着我会收到缺少文档的错误(对于这个特定项目来说是一个很好的提醒)。

但是,部分代码是由自定义工具生成的,该工具不会插入 xml 文档,因此我希望仅忽略生成的代码(而不是整个项目)缺少的 xml 文档。我对生成的实际文件没有影响,并且无法真正在文件中插入任何内容(因为它经常由工具重新生成),因此我寻找生成文件之外存在的内容(生成的类是部分的,如果有帮助的话)

I have turned on "Treat warnings as errors" for my VS project which mean that I get errors for missing documentation (nice reminder for this particular project).

However, part of the code is generated by a custom tool which does not insert xml documentation so I'm looking for away to ignore the missing xml documentation for the generated code only, not for the entire project. I have no influence on the actual file generated and cannot really insert anything in the file (as it is regenerated frequently by the tool) so I looking for something existing outside the generated file (the classes that are generated are partial, if that helps)

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

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

发布评论

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

评论(3

回忆躺在深渊里 2024-09-06 01:41:43

避免生成的代码出现警告或代码分析错误的最佳方法是使用 GenelatedCodeAttribute 并使代码文件以 *. generated.cs 模式结尾。

如果您的代码文件也有文件头,您应该有这些标签:

//----------------------
// <auto-generated>
//     Tool description
// </auto-generated>
//----------------------

这不是强制性的,但如果您有代码文件头,这是一个很好的做法。

这样,FxCop 和其他工具(如 StyleCop)将不再分析您的代码。

异常的是您的代码生成工具没有使用上述属性来装饰您的代码元素。尝试查看您的工具设置中是否有启用选项或联系开发团队。


编辑:
生成的类是否是部分类,并且实际的类名称和编号是否经常更改?因为如果生成的代码内容移动量不大,您可以做的就是创建另一个代码文件,并声明生成的分部类,并用 GenerationCodeAttribute 来装饰它们。有一次它救了我的命(还有我的时间!)。

The best way to avoid having warnings or Code Analysis error on generated code is to decorate your generated classes with the GeneratedCodeAttribute and make the code file ends with the *.generated.cs pattern.

If your code files also have a file header, you should had these tags:

//----------------------
// <auto-generated>
//     Tool description
// </auto-generated>
//----------------------

This is not mandatory, but if you have code file header it is a good practice.

This way, FxCop and other tools like StyleCop will not analyse your code anymore.

What is abnormal is that your code generation tool is not decortating your code elements with the attribute mentioned above. Try to look if there is an option to enable in your tool settings or contact the developing team.


EDIT:
Does the generated classes are partial classes and do the actual classes name and number changes often? Because if the generated code content is not moving a lot, what you can do is simply create another code file and just declare the generated partial class to decorate them with the GeneratedCodeAttribute. One time it saved my life (and my time!).

暗恋未遂 2024-09-06 01:41:43

编辑:请参阅表明这不适用于 C# 4 的注释。我不清楚它是否适用于早期版本的编译器。 C# 4 规范已经非常清楚地说明了这一点。第 2.5.8.1 节规定:

#pragma warning restore 指令将所有或给定的一组警告恢复到编译单元开始时有效的状态。请注意,如果从外部禁用了特定警告,#pragma warning recovery(无论是针对所有警告还是针对指定警告)将不会重新启用该警告。

Jeff 在博客文章中提供了解决方法 - 基本上是重新处理自动生成的代码作为构建的一部分。


正如 Tom 所说,您可以向整个项目添加“忽略”(构建/抑制警告 - 输入 1591 作为警告编号) - 但随后您可以在每个项目的顶部自行恢复警告。你的非生成文件:

#pragma warning restore 1591

它非常丑陋,但它有效(我刚刚测试过)。

EDIT: See comments indicating that this doesn't work with C# 4. I'm not clear whether it worked in earlier versions of the compiler. The C# 4 spec makes this pretty clear though. Section 2.5.8.1 states:

A #pragma warning restore directive restores all or the given set of warnings to the state that was in effect at the beginning of the compilation unit. Note that if a particular warning was disabled externally, a #pragma warning restore (whether for all or the specified warning) will not re-enable that warning.

Jeff has a workaround in a blog post - basically to reprocess autogenerated code as part of the build.


As Tom says, you can add an "ignore" to the whole project (Build / Suppress Warnings - enter 1591 as the warning number) - but then you can restore the warning yourself at the top of each of your non-generated files:

#pragma warning restore 1591

It's pretty ugly, but it works (I've just tested it).

李白 2024-09-06 01:41:43

您能做的最好的事情就是抑制包含生成代码的项目中的特定警告。您可以在“构建”选项卡上的“项目属性”中执行此操作。这并不理想,但除了将编译指示放入生成的代码中之外,它是您能做的最好的事情。

The best you will be able to do in this is suppress the particular warning in the project that contains the generated code. You can do this in the Project Properties on the Build tab. It's not ideal, but short of putting the pragmas in the generated code, it is the best you can do.

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