Friday, 1 September 2017

c# - StyleCop and FxCop rules exclude each other



I'm using StyleCop and FxCop tools to improve my code but I came to a place where there are two rules, one in StyleCop and one in FxCop that exclude each other. If I fix my code to match the rule from StyleCop then FxCop validation fails and vice versa.




First rule is SA1200 from StyleCop which says that all using directives must be placed inside of the namespace.




All using directives must be placed inside of the namespace.




So I have done something like this



namespace MyNamespace

{
using System;

...
}


It was ok for StyleCop, no more warnings. Now I run FxCop validation and it tells me that CA1014 is violated:





Mark 'MyApp.dll' with CLSCompliant(true) because it exposes externally visible types.




To resolve this I should do something like this:



[ClsCompliant(true)]
namespace MyNamespace
{
...
}



but now I cannot build my project because ClsCompliant attribute is not recognized (because it's from System namespace which I include inside of the MyNamespace). So if I move using System; directive outside of MyNamespace declaration. This will make my code compile but again it will break the rule from StyleCop.



Is there any way to deal with this problem except for disabling one of the rules in StyleCop or FxCop? And if that's not possible which of the rules should I disable? Which is less important?


Answer



Use full attribute name:



[System.CLSCompliant(true)]
namespace MyNamespace

{
...
}


BTW: if you want to mark your whole assembly as CLSCompliant, put



[assembly: System.CLSCompliant(true)]



in Properties/AssemblyInfo.cs file


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...