Can't load my custom rule

Sep 30, 2010 at 11:29 PM

I followed a bunch of examples, the documentation, and read the forums. I have an extremely simple rule that will not load into the settings. There is no logging so I cannot discover the quiet error. Everything is named InstanceVariablesUnderscorePrefix.something.  The target build platform is 3.5. Without any error message, I have no idea where to go next. The following code was shamelessly stolen from somebody else's example online.

using Microsoft.StyleCop;
using Microsoft.StyleCop.CSharp;

/// <summary>
/// This StyleCop Rule makes sure that instance variables are prefixed with an underscore.
/// </summary>
[SourceAnalyzer(typeof(CsParser))]
public class InstanceVariablesUnderscorePrefix : SourceAnalyzer {
    public InstanceVariablesUnderscorePrefix() {
    }

    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) {
        // Flag a violation if the instance variables are not prefixed with an underscore.
        if (!element.Generated && element.ElementType == ElementType.Field && element.ActualAccess != AccessModifierType.Public &&
            element.ActualAccess != AccessModifierType.Internal && element.Declaration.Name.ToCharArray()[0] != '_') {
            AddViolation(element, "InstanceVariablesUnderscorePrefix");
        }
        return true;
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Extensions">
  <Description>
    These custom rules provide extensions to the ones provided with StyleCop.
  </Description>
  <Rules>
    <RuleGroup Name="Formatting Rules">
    <Rule Name="InstanceVariablesUnderscorePrefix" CheckId="EX1001">
      <Context>Instance variables should be prefixed by underscore.</Context>
      <Description>Instance variables are easier to distinguish when prefixed with an underscore.</Description>
    </Rule>
    </RuleGroup>
  </Rules>
</SourceAnalyzer>

Thanks!

Developer
Oct 14, 2010 at 11:00 AM

Hi,

With the fear to seem annoying I will recommend you taking look at http://stylecopplus.codeplex.com
It allows a great flexibility in naming rules (and supports the rule you mentioned as well).

Considering your very example I could try to help you a bit later (in 2 weeks), if you still be insterested.

Best regards,
Oleg Shuruev 

Oct 19, 2010 at 4:19 PM

I downloaded it and have it installed. Nice. Seems to work reasonably well for a beta. :-) It has all the additional rules that I need to adjust for my company's style rules. Thanks for the pointer!

Developer
Oct 26, 2010 at 7:16 PM

Hi again,

As promised, I can give you little help now.

If you really want to learn making your own rules, just e-mail (or share in any other way) me your entire project (csproj file among with others).
I will inspect this and tell you the exact reason why it doesn't work.

Best regards,
Oleg Shuruev

Oct 26, 2010 at 8:40 PM

Thanks, but your stylecop+ works for every single change that I needed to have so I don't need to bother with getting my code to work. If I do need the help later, I'll ask again.

Thank you!

 

--Sean

Nov 4, 2010 at 4:05 AM

Hi there!
 I am also encountering problem on installing stylecop custom rule i created. It seems that Settings.StyleCop does not load the the custom rule. I've followed all the solutions presented in codeplex forums and others as well but still, it doesn't work. Can you help me out with this?

Thanks a lot.

Developer
Nov 5, 2010 at 5:38 PM
spongebob21 wrote:
Can you help me out with this?

Sure,

Just e-mail me your entire project (as I wrote above).

Best regards,
Oleg Shuruev

Developer
Nov 6, 2010 at 1:07 AM
Edited Nov 6, 2010 at 1:07 AM

Where are you putting your dll? Try putting it next to the built-in rule dll.

/Bo

Nov 8, 2010 at 12:28 AM

@BoLund: I've already place it next to the built-in rule at C:\Program Files\Microsoft StyleCop 4.4.0.14, but still it doesn't work..

@shuruev: Ok, i'll send you the project.

Developer
Nov 8, 2010 at 6:11 AM

Hi again,

I've reviewed your project very attentively and technically it's OK.
It can be successfully loaded as add-in and displayed in settings window.
I even tried how your rule works and it is quite workable too (at least it performs what you ask it to perform).

So it would be nice if you specify in more details what your problem is.
For example, if you try the following:
- take StyleCopCustomRule.dll from your bin folder (from the archive you sent)
- place it to C:\Program Files\Microsoft StyleCop 4.4.0.14
- drag & drop Settings.StyleCop to StyleCopSettingsEditor.exe
you should see your rule displayed in the settings window.
Could you please try it?

The other thing I would like to pay your attention to, is to take a look at StyleCop+ (already mentioned above in this thread).
It allows a great flexibility in naming rules (and supports the rule you are creating as well).

Best regards,
Oleg Shuruev

Nov 9, 2010 at 5:01 AM

At last it work! Thanks for the great help shuruev. Yeah you're right, my project is ok.

The only reason why my custom rule is not being loaded is that I didn't drag and drop Settings.StyleCop to StyleCopSettingsEditor.exe instead I just double-click the Settings.StyleCop.

Thanks again..

Ok, I'll try StyleCOp+ soon.

Nov 9, 2010 at 8:11 AM

By the way Shuruev, does stylecop reports supports stylesheet? if yes, how can i apply a stylesheet for the stylecop report that is being generated?

Thanks again..

Developer
Nov 30, 2010 at 6:04 AM

As far as I understand, StyleCop can only create output XML file.

It doesn't apply any XSLT to it, but it doesn't seem a big deal to do it by your own.

Best regards,
Oleg Shuruev

Dec 13, 2010 at 11:16 AM

Hi Shuruev,

i have created a custom dll with the above mentioned Code (First Thread)

But still i  am not able to see the Rule in StyleCop.Setting . Pls. help.

Mar 15, 2012 at 6:18 PM

Shuruev,

I did all steps explanied above, and my custom rule doesn't show in windows setting.

Can you help me?

Developer
Mar 18, 2012 at 4:33 PM

Hi guys,

Yes, I would like to help.

As this question raised quite often, I have created simple step-by-step guide for custom StyleCop rules creation:
http://stylecopplus.codeplex.com/wikipage?title=How%20to%20Create%20StyleCop%20Custom%20Rule

Could you please check it out and give any feedback whether it was useful enough to solve your issues? 

Thanks a lot!

Best regards,
Oleg Shuruev

Mar 20, 2012 at 10:27 PM

@dsscaze: If you exactly follow the steps explained in the WIKI, you will succeed. Important is, that you double check the versions of StyleCop installed on your machine and the reference in your custom rules project must be exactly the same. Otherwise the extension just doesn't get loaded. The xml where you define your rules must also follow all the rules explained. I struggled also a lot in the beginning.

If all of that doesn't help, try using Sysinternal's ProcessMonitor or Fusion Log (fslogvw from a VS command prompt). This might help identifying the problem.

Good Luck

Thomas

Jul 5, 2013 at 5:55 AM
Hi Shuruev,

I tried the same which u mentioned above,

//So it would be nice if you specify in more details what your problem is.
For example, if you try the following:
  • take StyleCopCustomRule.dll from your bin folder (from the archive you sent)
  • place it to C:\Program Files\Microsoft StyleCop 4.4.0.14
  • drag & drop Settings.StyleCop to StyleCopSettingsEditor.exe
    you should see your rule displayed in the settings window.
    Could you please try it?
The other thing I would like to pay your attention to, is to take a look at StyleCop+ (already mentioned above in this thread).
It allows a great flexibility in naming rules (and supports the rule you are creating as well).

//

I have installed StyleCop4.7 so am pasting dll in C:\Program Files (x86)\StyleCop4.7 folder. I did the project in 3.5 framework. My class file name and xml file name are also the same. I tried the same program which was given in "Documentation" tab in Stylecop website which you referred. But still my rule is not appearing in settings....