Share via


FAQ: How do I integrate custom rules with Visual Studio? [David Kean]

Update:  To have rules target both Visual Studio and FxCop, see the following entry: FAQ: Can I create custom rules that target both Visual Studio and FxCop?

 

Although not officially supported or documented, the Managed Code Analysis (FxCop) feature available with Visual Studio Team System comes with a rich API for writing custom rules.

 

There are a number of samples and articles on the web that already cover using this API and its usage with the externally available FxCop, so instead of covering the same information, this entry will instead give a quick rundown on integrating existing custom rules with Visual Studio.

 

Before beginning, download the attached zipped project (at the bottom of the post) and extract the contents. This project contains a single custom rule called PrivateFieldsShouldHaveCorrectPrefix that performs a simple check to make sure that private class fields (both instance and static) are prefixed with an underscore. The following code shows the type of field names it fires violations against (and those it doesn’t):

 

public class Class1
{
    private int _Field;
    private int _field;
    private int Field;   // Violates PrivateFieldsShouldHaveCorrectPrefix
    private int field;   // Violates PrivateFieldsShouldHaveCorrectPrefix
    private int m_field; // Violates PrivateFieldsShouldHaveCorrectPrefix
}

 

As the source has been provided, you can change PrivateFieldsShouldHaveCorrectPrefix to follow your own naming rules or add additional rules to the library to enforce your own company’s guidelines.

 

To register the rule library with Visual Studio, the built assembly (in this case, Microsoft.FxCop.Rules.Samples.dll) should be placed in the Code Analysis Rules folder, which by default is located at C:\Program Files\Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\Rules. Once the library has been registered, all opened projects (new and existing), will have any rules that are contained in the assembly enabled by default. You can now simply run Code Analysis over a project to analyze it with your new rules.

 

Before attempting this there are a couple of things you should be aware of:

  • You must have either Visual Studio Team Edition for Software Developers or Visual Studio Team Suite, as only these editions include the Code Analysis tools.

  • Rules or their base classes must extend BaseIntrospectionRule.

  • The rule description XML file (provided in the sample download) should be an embedded resource and end with ‘Rules.xml’ (case sensitive).

  • Rule libraries need to be compiled in .NET 2.0 and against the versions of Microsoft.Cci.dll and FxCopSdk.dll that ship with Visual Studio. These are located in C:\Program Files\Visual Studio 8\Static Analysis Tools\FxCop by default.

  • The SDK is currently undergoing a massive change, so any rules you write now for Visual Studio 2005, will not work in future versions, however, it should be relatively painless to convert them over.

     

Please note that this is not supported by Microsoft so please don’t call Product Support Services if you have any troubles, however, feel free to post any issues you encounter on the FxCop forum.

FxCopRuleSamples.zip

Comments

  • Anonymous
    March 13, 2006
    Rob Caron links to a brief Team System history lesson,Test Driven Development with Team System, the Team...
  • Anonymous
    March 14, 2006
    It's doomsday certain that my skateboard finatic son would call them rulz, but I figured that I would...
  • Anonymous
    March 14, 2006
    At the Security Summit in Perth yesterday, I was asked about adding rules to FxCop. Coincidentally, the...
  • Anonymous
    March 29, 2006
    I don't want to do a custom rule but I want our project to all use the same subset of rules.  Is there any easy way to specify that short of everyone doing there own configuring?
  • Anonymous
    March 29, 2006
    Hi Andy,

    Unfortunately, there is no support for automatically deploying custom rules to different projects and enabling these rules automatically. We're looking in to improving this in future versions of Visual Studio.

    Regards,

    Jeffrey
  • Anonymous
    March 30, 2006
    Jeffrey,

    Thanks.  Just to be sure I'm clear, I am NOT talking about custom rules.  I want to use the standard rule set but define which rules are on/off and have that same set applied easily by everyone on the project.  Your answer may be the same but I just need to take the shot.
    Thanks again - Andy
  • Anonymous
    April 11, 2006
    Andy,

    You could define a common piece of MSBuild that turns the appropriate rules on and off (set the CodeAnalysisRules property - you'll see an example in a .csproj that has rules turned off), and include that in your various .csproj/.vbproj files via an Import directive.

    (You may need to modify a registry key [you'll find out when you do it, and get help on it at the time] to get VS to shut up about the security of pulling in arbitrary bits of MSBuild.)

    We use this to override the set of rules .dlls that the built-in FxCop uses, that we can avoid having to put our custom rules into VSINSTALLDIR, and can keep them, appropriately version controlled, with our real source code.

    Cheers,
    Royston.
  • Anonymous
    April 11, 2006
    Andy & Royston,

    Keep a watch out on the blog over the next couple of weeks. We hope to post a solution to make this easier.

    Regards

    David
  • Anonymous
    April 19, 2006
    DavidKean_MS (Moderator): We'll be starting our chat about Visual Studio Team Edition for Software Developer...
  • Anonymous
    May 30, 2006
    I have heard that after right clicking the project -> properties, we should get a rules tab in properties window. On that tab we can enable fxcop and rebuild the project. Afetr rebuilding it should show the errors related to fxcop.
    I am not getting the rules tab in VSTS client. Can anybody help ?
  • Anonymous
    May 31, 2006
    I have users using both Visual Studio and FxCop, and I want to create custom rules that run on both....
  • Anonymous
    September 15, 2006
    許多先進 在看過 Code Analysis 的效益後, 下一個問題通常就是 "我想加自己組織專用的 遊戲規則, How to ?" How To 在哪裡 ? 小弟 101 句標準回答就是 請上 FxCop...
  • Anonymous
    April 13, 2008
    С Visual Studio Team System Developer Edition поставляется довольно богатый набор правил анализа кода,