如何抑制对生成代码的代码分析? [英] How to suppress code analysis on generated code?

查看:33
本文介绍了如何抑制对生成代码的代码分析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我做错了什么?

解决方案

也许您应该尝试适用于 StyleCop 的解决方案:

<块引用>

  1. 放入.Designer.cs"到您不希望 StyleCop 检查的文件名称的末尾.或者调用 [sic] 类,以及包含它的文件NativeMethods".确保您还取消选中分析设计器文件".在 StyleCop 设置中.在这种情况下,整个文件将被绕过.对于某些类型的 Microsoft 设计器生成的代码(例如 Windows 窗体设计器),您不必这样做,因为它们自动属于以下选项的条件:

  2. 用包含生成代码"的 C# 区域包围不需要的代码段以其名义.默认情况下,StyleCop 不检查生成的代码(但请确保未选中分析生成的文件"设置).在这种情况下,您仍然可以验证为 Windows 窗体控件生成的字段的名称.

#region Windows 窗体设计器生成的代码...#endregion

  1. 要忽略整个生成的文件,请检查您的生成器是否放置了";XML 元素插入 StyleCop-conform 文件头,如下所示:

//<自动生成/>

  1. 最后,您可以将ExcludeFromSourceAnalysis"设置为 trueMSBuild Compile 项的属性,表示需要从分析中排除的文件.它仅在您使用提供的Microsoft.SourceAnalysis.Targets"时才有效.目标文件,否则您必须自己为 StyleCop MSBuild 任务提供所需的源文件.

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

I have a Silverlight project with a generated Reference.cs file where the service reference is in. The class is attributed with [GeneratedCode] and in the project configuration the code analysis on generated code is disabled (Release and Debug).

What have I done wrong?

解决方案

Maybe you should try the solutions that work for 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.

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

这篇关于如何抑制对生成代码的代码分析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆