How to disable a particular compiler warning for a particular file

Daryl picture Daryl · Jul 12, 2012 · Viewed 7.3k times · Source

Background

I'm working on a small coding project that is going to be sold to other companies. I needed to create some documentation for it, so I decided to use Sandcastle. After taking far to long to download and install, I finally got it working, and noticed any public method or class that didn't have a comment had red text stating that the comment was missing. I then installed Ghostdoc to help speed up my commenting. This turned on the compiler warnings for missing xml comments, which was great because I now had a list of everything I needed to comment.

The Problem

One of my code files is an auto-generated file, which contains around 3000 compiler warnings. I need to be able to skip that file from creating any "Missing Xml Comment" compiler warnings. I know these things from this post:

  • I know I can turn off the compiler warning for the project, but there are other files in the project that should have the compiler warning.
  • I know I can use #pragma warning disable 1591 to remove the compiler warning, but the file is autogenerated, and I really don't want to have to re-add it manually every time.
  • I know I can add an empty comment, but once again, I really don't want to have to re-add it every time the file gets regenerated.
  • I could pull the file out into it's own project since it is the only class in it's namespace, then remove the XML comment requirement, but I don't want the customer to have to deal with another dll.
  • The classes are partial classes so I was thinking about trying to find a way to add the #pragma warning disable in a partial class but even if that was possible, there are still enums that would throw warnings.

How can I tell VS to ignore a single file for a particular type of warning?

Answer

David Thielen picture David Thielen · Jul 12, 2012

A couple of possibilities spring to mind:

  1. Can you have another class that imports the auto-generated .cs file? The wrapping class has the pragma and just imports the autogenerated class.
  2. Write a perl script (or simple C# program) that is called as a build event after the file is generated and before the .cs files are compiled.