2

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.

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. The file content will be replaced by the xsd.exe the next time I run it. 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).

But .NET seems to have a builtin mechanic for this case: SuppressMessageAttribute at assembly level (Microsoft Docs: Suppress warnings).

So I went and created a file GlobalSuppressions.cs with the following content:

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

walkslowly
  • 417
  • 1
  • 4
  • 16
  • It's not at all clear why suppressing the message with a pragma directive in the source file won't work for you. – Mark Benningfield Nov 29 '22 at 15:14
  • Because the source file is generated by the 'xsd.exe'. Regenerating the source code from my schema will just remove what I previously added to the file - as I already wrote in my question (maybe a bit unclear, gonna edit that). – walkslowly Nov 29 '22 at 15:28
  • Well, presumably you have the code generation step automated in the build process. Simply add another step, take the output from the xsd.exe tool, add the pragma directive, and resave the file. – Mark Benningfield Nov 29 '22 at 15:36
  • Actually right now it's a target which i can enable/disable with a property. Occasionally I regenerate the source with a make or cake script. I am starting to figure that this might be the only solution here. But I was hoping that I may be able to utilize SuppressMessageAttribute for this because it seemed to me the "official" way. – walkslowly Nov 29 '22 at 15:39
  • 1
    I have found over the years that most of the time the "official way" is more of a pain in the ass than it's worth, depending on who the "official" is. – Mark Benningfield Nov 29 '22 at 15:41
  • Sad, but true... Starting to realize this more and more, too. Think I might just gonna add it to make/cake then. Thanks for the input! – walkslowly Nov 29 '22 at 15:45
  • 1
    Note: There's no need for an Edit Log on questions. When you edit the question, you can specify an Edit Summary. Anyone interested can view what changed, and the Edit Summary by clicking the "edited x time ago" link or by viewing the timeline. – Heretic Monkey Nov 29 '22 at 17:25

1 Answers1

2

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. 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
  2. add .editorconfig to that directory
  3. configure message severity in .editorconfig as desired

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

# 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. But I wanted to keep the generated file in the repository. This will possibly be changed later on.

walkslowly
  • 417
  • 1
  • 4
  • 16