如何在生成的代码上禁用代码分析?

10

我有一个Silverlight项目,其中包含一个生成的Reference.cs文件,服务引用就在这个文件中。该类带有[GeneratedCode]属性,并且在项目配置中已禁用了对生成代码的代码分析(发布和调试都是如此)。

我做错了什么?


请问如何配置StyleCop以在生成的代码上抑制警告? - Jader Dias
1个回答

14
也许你应该尝试适用于StyleCop的解决方案:
  1. Put ".Designer.cs" to the end of the name of the file you don’t want StyleCop to check. Or call the the [sic] class, and the file containing it, "NativeMethods". Make sure you also uncheck "Analyze designer files" in StyleCop settings. In this case the whole file will be bypassed. You don’t have to do so for some types of Microsoft designer-generated code, like Windows Forms Designer, because they automatically fall under conditions of the following option:

  2. Surround the undesired piece of code with a C# region containing "generated code" in its name. StyleCop does not check generated code by default (make sure the "Analyze generated files" setting is not checked, though). In this case you can still validate the names of the fields generated for the Windows Forms controls.

#region Windows Form Designer generated code
    ...
#endregion
  1. To ignore the whole generated file, check whether your generator puts an "<auto-generated />" XML element into the StyleCop-conform file header, like the following:
// <auto-generated />
  1. And finally, you can set to true the "ExcludeFromSourceAnalysis" property of the MSBuild Compile item that represents the file needed to be excluded from analysis. It only works if you use the provided "Microsoft.SourceAnalysis.Targets" targets file, otherwise you have to feed the StyleCop MSBuild task with desired source files on your own.

来源:https://shishkin.wordpress.com/2008/07/08/stylecop-how-to-ignore-generated-code/


1
我尝试了选项3,但自动生成的标签被忽略了 - 我添加了一个关于此的问题:https://dev59.com/N2445IYBdhLWcg3wH2vH - Daniel Daranas
4
代码分析(曾用名FxCop)与StyleCop并不相同,也许“Cop”这个词容易引起混淆。 - Juan Zamudio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接