Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Saturday, March 11, 2006 9:16 PM by David M. Kean
Attachment(s): FxCopRuleSamples.zip

Comments

Saturday, March 11, 2006 4:36 PM by Managed from down under

# Integrating Custom Rules with Visual Studio

Sunday, March 12, 2006 7:08 AM by Jason Haley

# Interesting Finds

Monday, March 13, 2006 9:55 AM by Team System News

# VSTS Links - 3/13/2006

Rob Caron links to a brief Team System history lesson,Test Driven Development with Team System, the Team...
Tuesday, March 14, 2006 1:40 PM by Federal Developer Weblog

# Static Code Analysis Mini-Roadmap for Custom Rules (formerly FxCop)

It's doomsday certain that my skateboard finatic son would call them rulz, but I figured that I would...
Tuesday, March 14, 2006 9:41 PM by Andrew Coates ::: MSFT

# How do I add rules to FxCop?

At the Security Summit in Perth yesterday, I was asked about adding rules to FxCop. Coincidentally, the...
Wednesday, March 29, 2006 4:26 PM by AndyPick

# re: Integrating Custom Rules with Visual Studio Team System [David Kean]

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?
Wednesday, March 29, 2006 4:45 PM by Jeffrey van Gogh

# re: Integrating Custom Rules with Visual Studio Team System [David Kean]

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
Thursday, March 30, 2006 9:40 AM by AndyPick

# re: Integrating Custom Rules with Visual Studio Team System [David Kean]

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
Tuesday, April 11, 2006 12:54 PM by Royston Shufflebotham

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

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.
Tuesday, April 11, 2006 1:02 PM by David M. Kean

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

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
Wednesday, April 12, 2006 6:29 AM by Lorenzo Barbieri @ UGIblogs!

# WebCast sull'Estensibilit

Wednesday, April 19, 2006 2:15 PM by Team System News

# February 15th, 2006 Chat Summary - Visual Studio Team Editio for Software Developer and Software Tester Tools

DavidKean_MS (Moderator): We'll be starting our chat about Visual Studio Team Edition for Software Developer...
Monday, May 15, 2006 6:43 AM by Lorenzo Barbieri @ UGIblogs!

# Webcast di oggi sull'estensibilit

Tuesday, May 30, 2006 8:34 AM by Deeps

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

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 ?
Thursday, June 01, 2006 3:15 AM by Visual Studio Managed Code Analysis (FxCop)

# FAQ: Can I create custom rules that target both Visual Studio and FxCop?

I have users using both Visual Studio and FxCop, and I want to create custom rules that run on both....
Friday, September 15, 2006 8:52 AM by Refines.Info["Polo Lee"]

# 自訂 Code Analysist Rule 遊戲規則

許多先進 在看過 Code Analysis 的效益後, 下一個問題通常就是 "我想加自己組織專用的 遊戲規則, How to ?" How To 在哪裡 ? 小弟 101 句標準回答就是 請上 FxCop...
Sunday, April 13, 2008 11:46 AM by Дмитрий Лапшин

# Создание собственных правил статического анализа кода

С Visual Studio Team System Developer Edition поставляется довольно богатый набор правил анализа кода,

New Comments to this post are disabled
 
Page view tracker