如何禁用特定文件的特定编译器警告

背景

我正在开发一个小型编码项目,该项目将被出售给其他公司。 我需要为它创建一些文档,所以我决定使用Sandcastle。 经过很长时间的下载和安装后,我终于开始工作,并注意到任何没有评论的公共方法或类都有红色文字说明评论缺失。 然后我安装了Ghostdoc来帮助加快我的评论速度。 这打开了编译器警告缺少xml注释,这很好,因为我现在有一个我需要评论的所有内容的列表。

问题

我的一个代码文件是一个自动生成的文件,其中包含大约3000个编译器警告。 我需要能够跳过该文件来创建任何“Missing Xml Comment”编译器警告。 我从这篇文章中了解到这些:

  • 我知道我可以关闭项目的编译器警告,但项目中还有其他文件应该有编译器警告。
  • 我知道我可以使用#pragma warning disable 1591删除编译器警告,但该文件是自动生成的,我真的不想每次都要手动重新添加它。
  • 我知道我可以添加一个空注释,但是再一次,我真的不想在每次重新生成文件时重新添加它。
  • 我可以把文件放到它自己的项目中,因为它是它的命名空间中唯一的类,然后删除XML注释要求,但我不希望客户必须处理另一个dll。
  • 这些类是部分类,所以我正在考虑尝试找到一种方法在部分类中添加#pragma warning disable,但即使可能,仍然会有枚举警告。

如何告诉VS忽略特定类型警告的单个文件?

我想到了几种可能性:

  1. 你能有另一个导入自动生成的.cs文件的类吗? 包装类具有pragma并只导入自动生成的类。
  2. 编写一个perl脚本(或简单的C#程序),在生成文件之后和编译.cs文件之前,它被称为构建事件。

如果生成的类不应该对用户可见,则可以检查生成工具是否具有将类生成为内部而非公共的选项。

如果生成的代码是Web服务引用,则可以在创建引用时指定此参数(在“添加服务引用”对话框中,“高级” – >“生成的类的访问级别”)。

否则,您可以尝试找到一种方法来自动更改生成的代码中的类型的访问级别,从公共到内部。

编辑:我的解决方案

我使用了David Thielen的建议并创建了一个C#程序,它将#pragma warning disable messages插入到我自动生成的文件中。 理想情况下,我将其称为文件生成本身的后期操作,但是现在预编译命令就足够了,因为我的语句将是文件中的第一行,它只需要阅读一些看到禁用语句已经存在,并退出,所以它不应该减慢构建速度。 以下是我的节目供大家欣赏! 🙂

 ///  /// Addes #pragma warning disable messages to source code as part of a prebuild to ignore warnings. /// Primarly used for autogenerated classes that may contain some compiler warnings by default ///  public class Program { ///  /// ///  ///  /// [0] - file to edit /// [1] - line number to insert / edit warning disable at /// [2+] - warnings numbers to disable static void Main(string[] args) { // Preconditions if (args.Length < 2) { throw new ArgumentException(String.Format("Unexpected number of parameters.{0}Parameters should be [0] - file to edit{0}[1] - line number to insert / edit warning disable at{0}[2+] - warnings numbers to disable", Environment.NewLine)); } else if (args.Length == 2) { return; } // Valid number of args, validate arguments string filePath = args[0]; long lineNumber; if(!long.TryParse(args[1], out lineNumber)){ throw new InvalidCastException("Unable to cast \"" + args[1] + "\" to a long"); } string[] compilerWarningNumbers = new string[args.Length - 2]; Array.ConstrainedCopy(args, 2, compilerWarningNumbers, 0, compilerWarningNumbers.Length); // File Name and line number are valid, perform search and replace AddOrUpdateCompilerWarningDisabler(filePath, lineNumber, String.Join(",", compilerWarningNumbers)); } private const string TEMP_FILE_POSTFIX = ".CompilerWarningDisabler.txt"; public static void AddOrUpdateCompilerWarningDisabler(string filePath, long lineNumber, string compilerWarningNumberCSV) { if (!File.Exists(filePath)) { throw new FileNotFoundException("File path not found!", filePath); } // Set Clear Readonly Flag FileInfo fileInfo = new FileInfo(filePath); bool isReadOnly = fileInfo.IsReadOnly; // Get Temp File Name and Delete if it already exists string tempFile = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + TEMP_FILE_POSTFIX); File.Delete(tempFile); // Read from the target file and write to a new file. int currentLine = 1; string line; string textToWrite = "#pragma warning disable " + compilerWarningNumberCSV; try { using (StreamReader reader = new StreamReader(filePath)) using (StreamWriter writer = new StreamWriter(tempFile)) { while ((line = reader.ReadLine()) != null) { if (currentLine == lineNumber) { if (line.StartsWith("#pragma warning disable")) { if (line == textToWrite) { // Nothing has changed, don't bother copying file return; } else { line = textToWrite; } } else { writer.WriteLine(textToWrite); writer.WriteLine(line); } } else { writer.WriteLine(line); } currentLine++; } if (currentLine == lineNumber) { writer.WriteLine(textToWrite); } if (currentLine < lineNumber) { throw new InvalidDataException("File " + filePath + " does not contain line number " + lineNumber); } } // This could potentially delete the source file, but this should be messing with autogenerated files, so even if it does happen, it shouldn't be to hard to get it back if (isReadOnly) { fileInfo.IsReadOnly = false; } File.Delete(filePath); File.Move(tempFile, filePath); if (isReadOnly) { new FileInfo(filePath).IsReadOnly = true; } } finally { File.Delete(tempFile); } } }