Domanda

We have an existing product where we would like to implement the usage of StyleCop. However, we have one problem with this and it's that all our namespaces starts with lower-case (for instance lowerCase.UpperCase.Etc.Etc.) and ofcourse that leads to that the SA1300 (Element Must Begin With Upper Case Letter) rule gets broken at every file.

Although, it's impossible to change the namespaces at this point because it's already out there and we have many partners and customers which already implement interfaces against our api and is depended on a stable namespace. I'm sure you understand the consequence of a change...

We like to have the rule enabled in general, but temporary disabled on the namespace line. Have tried with:

[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Reviewed. Suppression is OK here.")]

And it works if I put it above the namespace line, however it will basically disable the rule completely, which is not what we want because we like the rule on classes and class members...

I would like to have something like:

#pragma warning disable SA1300
namespace lowerCase.UpperCase.Etc
{
#pragma warning enable SA1300

But SA1300 is no such type of warning, as I understand.

Anyone has some ideas of how to solve this problem?

Thanks in advance!

È stato utile?

Soluzione

There is a workaround.

You can write like this:

#region Generated Code
namespace lowerCase.UpperCase.Etc
{
#endregion

As designed, Stylecop will ignore everything within the region because you have thereby stated that "you don't own the code and can't therefore take responsibility for the style".

However there is one remark: all rules will be ignored within the region, so make sure you just have the "unwanted" rule before you put the region in place.

Origin of this is actually a small comment (not even an answer) on another question: Disable StyleCop for specific lines

Altri suggerimenti

Unfortunately, what you want to do isn't possible. However, StyleCop+ includes a more flexible version of the naming rule that would allow you to specify different formats for namespace names than for other names.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top