locked
VS2005 Custom Rules: How to enable individual selection?

    Question

  • I've ported some custom rules over from FXCop/VS2003. They compile and run just fine.

    But my problem is that in the "Code Analysis" settings options page, the little [+] next to the rule category doesn't expand to show my individual rules, so I can't turn them on and off individually.

    How can I get it to work?

    My XML file for the rules is as follows:


    <Rules FriendlyName="Prevent Enum.ToString()">
        <Rule TypeName="BoxingEnums" Category="Dmr.FxCop.Rules" CheckId="DMR0001">
            <Name>Do not pass an enum to string.Format().</Name>
            <Description>This rule fires any time an enum is boxed. This enables you to find cases where an enum value is being passed to string.Format() which will result in enum.ToString() being called. This will break under Dotfuscation.</Description>
            <Resolution>Method {0} is boxing enum {1}. This may be because it is being passed to string.Format() or any other function that can result in enum.ToString() being called. This would cause problems when Dotfuscating! Ensure that the function to which the enum is being passed does not directly or indirectly call enum.ToString().</Resolution>
            <MessageLevel Certainty="65">Warning</MessageLevel>
            <FixCategories>DependsOnFix</FixCategories>
            <Owner />
            <Url />
            <Email />
        </Rule>
        <Rule TypeName="EnumToString" Category="Dmr.FxCop.Rules" CheckId="DMR0002">
            <Name>Do not convert an enum value to a string using built-in enum methods.</Name>
            <Description>This rule fires when an enum value is converted to a string using an enum method.</Description>
            <Resolution>Method {0} calls {1}. This will cause problems when Dotfuscating. You should not use enum.ToString(), enum.GetNames(), enum.GetName(), enum.Format() or enum.Parse() to convert an enum to a string. Instead, use a switch statement, and also internationalize the names if necessary.</Resolution>
            <MessageLevel Certainty="95">Error</MessageLevel>
            <FixCategories>DependsOnFix</FixCategories>
            <Owner />
            <Url />
            <Email />
        </Rule>
    </Rules>

     


    And my base class for my rules looks like this:

    namespace Dmr.FxCop.Rules
    {
        [CLSCompliant(false)]
        abstract public class IntrospectionTestRule : BaseIntrospectionRule
        {
            protected IntrospectionTestRule( string name ):
                base( name, "Dmr.FxCop.Rules.DetectEnumToString", typeof(IntrospectionTestRule).Assembly )
            {
                // Nothing else to do.
            }
        }
    }

     


    Has anyone got any ideas? Thanks!
    Thursday, November 03, 2005 11:46 AM

Answers

  • Hi Matthew,

    Are you trying to load these rules inside Visual Studio 2005? If so, you're right that your change in xml file name let to the rules being loaded in the UI. The UI doesn't create instances of the rules, but opens up the first resource in the assembly it finds that ends in "Rules.xml". This is not required in the standalone of fxcop as we always load the rules in to memory and ask the rules for their information, that then know which resource to retrieve this from.

    Regards,


    Jeffrey
    Friday, November 04, 2005 10:02 PM

All replies

  • Well, this is strange. I got it working by changing the name of the resource file from "DetectEnumToString.xml" to "EnumRules.xml". It doesn't really make sense that should fix it, but ho hum.

    The DLL name was "DetectEnumToString.dll" - so I wonder if having the resource name the same could have affected it? Seems unlikely. But what else could it be?
    Thursday, November 03, 2005 2:39 PM
  • Hi Matthew,

    Are you trying to load these rules inside Visual Studio 2005? If so, you're right that your change in xml file name let to the rules being loaded in the UI. The UI doesn't create instances of the rules, but opens up the first resource in the assembly it finds that ends in "Rules.xml". This is not required in the standalone of fxcop as we always load the rules in to memory and ask the rules for their information, that then know which resource to retrieve this from.

    Regards,


    Jeffrey
    Friday, November 04, 2005 10:02 PM
  • Yikes. Thanks for this clarification, Jeffrey, I was entirely off in my post.
    Friday, November 04, 2005 10:04 PM
  • Ah, thanks Jeffrey - that explains it! That's fine - I know what to do now. Big Smile

    It was kind of luck that I renamed it to something that ended in "Rules" - I had an example (from Wintellect) that worked, and I was changing mine to match it. Theirs ended in "Rules" so I made mine end in "Rules" too without really thinking about it much...
    Saturday, November 05, 2005 12:25 AM