No rules in your FxCop rule assembly?

Since I posted an FxCop rule sample over at, it’s rapidly become the most popular content on the site. Not something I expected but, given that I have trouble coming up with blogging topics and there seems to be some interest in FxCop, I figured I might as well spend a lovely Saturday morning writing about it… (Actually, I’m pretty much just killing time while waiting for a tire change, so please feel free to keep that “get a life” comment to yourself. <g>)

There’s all sorts of stuff I could (and will try to) write about FxCop use, but one thing that jumped out at me this week was quite a few searches for “no FxCop rules could be found” landing on my rule sample page, so I guess I’ll start with that…

There are several reasons why FxCop might have trouble finding rules in a custom rule assembly:

  1. There are no rule classes.

    If you haven’t added any rule classes to your assembly, it shouldn’t be too much of a problem to figure out why FxCop can’t find them. 😉

    Another problem might be that your rule classes don’t implement the necessary rule interfaces. If you’ve done the usual thing and created your rules as subclasses of Microsoft.FxCop.Sdk.Introspection.BaseIntrospectionRule, then you’ve got the interface problem covered. However, if you’re rolling your own rules from scratch, you’re probably going to have to do quite a bit more work… (Which I’ve never done, so I have no idea what’s the minimum required. My first guess would be IRule, but that seems rather useless since there are no problem collections exposed until one gets to IControlFlowRule or IIntrospectionRule.)

  2. You’ve forgotten to add an embedded rules resource file.

    This is probably the most common problem for beginners. If you’re not sure what the rule resource file should contain, grab yourself a copy of Reflector, and take a look at the Microsoft.FxCop.Rules.Design.DesignRules.xml embedded resource in the DesignRules.dll file that ships with FxCop or Visual Studio Static Analysis.

    Also, remember that this file must be set to be an embedded resource. If the file doesn’t end up in your compiled assembly, FxCop won’t be able to find it.

  3. You’ve incorrectly identified the embedded rules resource file in which the data for your rule class can be found.

    Once you’ve created your embedded rules resource file, this may be the next problem you hit. If you’re inheriting from BaseIntrospectionRule, you must pass the full resource file name (minus the .xml extension) when invoking the protected BaseIntrospectionRule constructor. Since all the custom rules in a given assembly will typically use the same rules resource file, one would usually create a base class from which they would all inherit. The rule base class might look something like this:

    namespace Bordecal.FxCop.Rules
    	internal abstract class IntrospectionRule : BaseIntrospectionRule
    		protected IntrospectionRule(string name)
    			: base(name, "Bordecal.FxCop.Rules.Rules", typeof(IntrospectionRule).Assembly)

    This avoids requiring all rules to provide the resource file name, but it doesn’t help with figuring out what the file name ought to be in the first place.

    The bad news is that inferring the resource file name can be a bit tricky. It will depend on your project’s default namespace, as well as on where the rule file resides within your project’s directory hierarchy. The good news is that you don’t need to infer the name at all. Instead, simply open your rule assembly in Reflector, and copy the full name of your resource file (remember to omit the .xml extension):

One thought on “No rules in your FxCop rule assembly?

  1. Having the right versions of FxCopSdk.dll and Microsoft.cci.dll referenced.

    Typically in C:\Program Files\Microsoft FxCop [version]

    I’ve seen a few people (myself included) get stuck because they had a few copies of those DLLs (in Microsoft SDKs and Visual Studio folders) and used ones that were for a different version of FxCop, or downloaded a tutorial project that had FxCop DLLs for a different version for what they were using.

Leave a Reply

Your email address will not be published. Required fields are marked *