简体   繁体   English

生成代码 (xsd.exe) 的 SuppressMessage 编译器警告 CS1591

[英]SuppressMessage compiler warning CS1591 for generated code (xsd.exe)

I'm using the tool xsd.exe in several projects to generate classes for my data model. When turning on documentation file generation in my csproj the compiler starts to show lots of warnings of type: CS1591:Missing XML comment for publicly visible type or member pointing at generated constructors.我在几个项目中使用工具xsd.exe为我的数据 model 生成类。当在我的 csproj 中打开文档文件生成时,编译器开始显示许多类型的警告: CS1591:Missing XML comment for publicly visible type or member指向生成的构造函数的CS1591:Missing XML comment for publicly visible type or member

As this is kind of intended behavior I am trying to figure out how to suppress these warnings.由于这是一种预期的行为,我试图弄清楚如何抑制这些警告。 But only for the types generated by xsd.exe which are contained in a single source file.但仅适用于xsd.exe生成的类型,它们包含在单个源文件中。 The file content will be replaced by the xsd.exe the next time I run it.下次运行时文件内容将被xsd.exe替换。 Any modifications to the file will be lost in that process.对该文件的任何修改都将在该过程中丢失。 So adding a #pragma warning disable to the file is not a solution here (I sometimes even use a build target which regenerates the code on Build).因此,在文件中添加#pragma warning disable并不是解决方案(我有时甚至使用在构建时重新生成代码的构建目标)。

But .NET seems to have a builtin mechanic for this case: SuppressMessageAttribute at assembly level ( Microsoft Docs: Suppress warnings ).但是 .NET 似乎有针对这种情况的内置机制:汇编级别的SuppressMessageAttributeMicrosoft 文档:抑制警告)。

So I went and created a file GlobalSuppressions.cs with the following content:所以我去创建了一个包含以下内容的文件GlobalSuppressions.cs

[assembly: SuppressMessage("Compiler",
                           "CS1591:MissingXmlCommentForPubliclyVisibleTypeOrMember",
                           Justification = "Generated code",
                           Scope = "member",
                           Target = "M:Company.IO.Component.Concrete.Configuration.ConfigItem.#ctor")]

But the suppression is being ignored.但是压制被忽略了。

Anyone any ideas?有人有什么想法吗?

I found a solution without having to manipulate the generated source file.我找到了一个无需操作生成的源文件的解决方案。

Short短的

An .editorconfig can be placed in the directory of the generated source files. .editorconfig可以放在生成的源文件的目录中。 The rules contained in the file are applied to this directory and descendants only.文件中包含的规则仅适用于此目录和后代。

Long

  1. output generated code in a seperate directory containing only generated source files output 在仅包含生成的源文件的单独目录中生成代码
  2. add .editorconfig to that directory.editorconfig添加到该目录
  3. configure message severity in .editorconfig as desired根据需要在.editorconfig中配置消息严重性

In my case this looks as follows在我的例子中,这看起来如下

Directory structure目录结构

\Configuration
  \Schema
    \.editorconfig  # applied to code files in this directory only
    \schema.xsd     # template for xsd.exe
    \schema.cs      # generated by xsd.exe
Project.csproj
Code.cs

Content of .editorconfig .editorconfig的内容

# message severity for generated code files
[*.cs]
dotnet_diagnostic.CS1591.severity = none

This might have worked out better if I generated the code directly to the obj/ folder naming it something like schema.g.cs .如果我直接将代码生成到obj/文件夹并将其命名为schema.g.cs ,这可能会更好。 But I wanted to keep the generated file in the repository.但我想将生成的文件保留在存储库中。 This will possibly be changed later on.这可能会在以后更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM