can anyone help me to find the problem with my code?

Oct 20, 2010 at 5:57 PM

 

Hi I'm trying to create a custom rule, but it doesn't work.
My rule is : "add Delegate to the end of the name."
Please, can anyone help me to find the problem with my code?:
Thanks.
namespace Arkus.NamingConventions
{
    using Microsoft.StyleCop;
    using Microsoft.StyleCop.CSharp;
    using System.Globalization;

    /// <summary>
    /// Checks the names of code elements.
    /// </summary>
    [SourceAnalyzer(typeof(CsParser))]
    public class ArkusNamingRules : SourceAnalyzer
    {
        /// <summary>
        /// Initializes a new instance of the NamingRules class.
        /// </summary>
        public ArkusNamingRules()
        {
        }

        /// <summary>
        /// Checks the case of element names within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        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);

            }
        }
        public bool VisitElement(CsElement element, CsElement parentElement, object context)
        {
            if (!element.Generated)
            {
                if (element.ElementType == ElementType.Delegate && element.Declaration.Name.EndsWith("Delegate") == false)
                {
                    AddViolation(element, "Add 'Delegate' to the end of the name.");
                }
                
           }
            return true;
        }
    }
}

 

Developer
Oct 25, 2010 at 9:13 PM

First things first: do you know if your rule is running, or are you stabbing in the dark?

I had a workflow where I would temporarily add System.Diagnostics.Debugger.Launch to an early piece of the code I wanted to step through, and trigger that from a separate instance of VS (trying to have VS debug a module within it's own instance is a bad idea).  Placed somewhere that is early and guaranteed to run will additionally help you know if you copied over the correct StyleCop files, etc.  (This isn't necessarily the best workflow but I personally didn't want to invest in finding the perfect workflow since I didn't plan on coding here very often, at least not yet.)

Anyway, here's another recent thread talking about custom naming rules, and as mentioned there, maybe stylecopplus has what you need?  (I haven't even looked at it myself yet.)

http://stylecop.codeplex.com/Thread/View.aspx?ThreadId=229220

Developer
Oct 26, 2010 at 9:15 PM

Hi Cshark01,

First, I wanted to add that karak was absolutely right when said that "two-VS" debugging scenario was not the only one.
With the appropriate environment you could debug it in one VS or even without VS (as a unit-test) - it is a question of efforts.

Second, StyleCop+ is definitely can do what is requested here (thanks karak for the reference). So you could give it a try.

Third, if you completely lost in your tryings, you could email me (or share in any other way) your entire C# project.
I will inspect it and tell you why it doesn't work.

Best regards,
Oleg Shuruev