Sergey Shishkin

on agile software development

StyleCop: How To Ignore Generated Code

There are several possibilities to make StyleCop ignore some code:

  1. Put ".Designer.cs" to the end of the name of the file you don’t want StyleCop to check. Or call the the 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
  3. 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 />
  4. 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.

Drop me a line if I’m incorrect or did miss something.

Technorati Tags:
Advertisements

Written by Sergey Shishkin

08.07.2008 at 00:37

Posted in Uncategorized

4 Responses

Subscribe to comments with RSS.

  1. This will definately work, but it\’s not really a good solution. In all cases, you are falsely identifying your code as autogenerated or containing interop code. Yes, it tricks StyleCop into ignoring the file, but has a higher risk of leading to maintainability issues down the road.

    Scott

    01.01.2009 at 03:41

  2. Hi,Great blog with interesting informations. I can use it t solve my problem.ThanxM.http://www.vanjobb.hu/

    Unknown

    15.05.2009 at 13:07

  3. Hi,None of these possibilities apply when you\’re using DataSets in Web Application. If you have DataSet in a Web Application then you end up with generated file (for example dataset.xsd.72cecc2a.cs) which is not part of Web Application project. How would you solve this problem?Thanks!

    Slaven

    15.05.2009 at 14:23

  4. At http://www.progiftstore.com/ you can get the best quality swiss replica watches with the lowest price, in progiftstore, you can buy replica watches with a great discount.

    Jason

    02.12.2009 at 06:32


Comments are closed.

%d bloggers like this: