StyleCop 4.7.17 Custom Rules

Apr 3, 2012 at 8:00 AM

Hi all !

I have some problem to get Microsoft StyleCop to pick a new rule up. The new rule is not shown in the Settings.

I just did as said in the documentation _StyleCopSDK.htm

The assembly is compiled without errors.

AccessModifiers.cs and AccessModifiers.xml below

I'm using VS10 with SP1 and SC 4.7.17

Thanks for help in advance

--------------------------------------------------------------------------------------

namespace AccessModifiers
{
    // Checks for the presence of access modifiers on all elements.
    [SourceAnalyzer(typeof(CsParser))]
    public class AccessModifiers : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument document)
        {
            CsDocument csdocument = (CsDocument)document;
            if (csdocument.RootElement != null && !csdocument.RootElement.Generated)
            {
                csdocument.WalkDocument(
                    new CodeWalkerElementVisitor<object>(this.VisitElement),
                    null,
                    null);
            }
        }

        private bool VisitElement(CsElement element, CsElement parentElement, object context)
        {
            // Make sure this element is not generated.
            if (!element.Generated)
            {
                // Flag a violation if the element does not have an access modifier.
                if (!element.Declaration.AccessModifier)
                {
                    this.AddViolation(element, "AccessModifiersMustBeDeclared");
                }
            }
            return true;
        }
    }
     
}

 

----------------------------------------------------------------------------------------------------------------

AccessModifiers.xml

-----------------------------

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Extension">
  <Description>
    This is my first rule ever done.
  </Description>
  <Rules>
    <RuleGroup Name="MLOYRules">
      <Rule Name="AccessModifiers" CheckId="MY1000">
        <Context>What ever it is !!.</Context>
        <Description>Detail descrition.</Description>
      </Rule>
    </RuleGroup>
  </Rules>
</SourceAnalyzer>

Apr 3, 2012 at 8:56 AM

Hi all !

There is a really good instruction of how to make custom rules done by shuruev :

http://stylecopplus.codeplex.com/wikipage?title=How%20to%20Create%20StyleCop%20Custom%20Rule

Unfortunately I found that it works fine with .NET3.5 but not with 4.0. Actually the rule works fine with both, but

only with .NET3.5 the rule is visible in settings form.

If someone can confirm my findings it would be quite useful.

Thanks

savirpe

Coordinator
Apr 4, 2012 at 6:10 AM

Hey savirpe,

I think that is true while StyleCop is wirtten using .NET 3.5.

Best regards,
Oleg Shuruev