Disable StyleCop for specific lines

Viewed 18224

We're using StyleCop in our C# projects. In some cases we'd like to avoid the rules though. I know you can add // <auto-generated /> in the beginning of the file to make StyleCop ignore it. However, I don't want to ignore the rules for the whole file - only a block of code within it.

Can I disable StyleCop for specific lines somehow?

6 Answers

This guy seems to have a good general ignore hack; he suggests putting this at the top of the file - tested and working with R#

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// </auto-generated>
//------------------------------------------------------------------------------

Handy for when you are just churning out a load of boilerplate to adhere to a mainly unimplemented interface, for example.

You can disable a block of code like so:

// some other code before and after

#pragma warning disable SA1008 // Opening parenthesis must not be preceded by a space
     (_, value) => (key, value)))
#pragma warning restore SA1008
Related