Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Monitor .NET Code Quality with FxCop and Custom Rules : Page 3

You no longer have to rely solely on experience and code reviews to find code problems; FxCop can find many problems for you. It even lets you create and enforce custom rules that apply specifically to your organization's code.


advertisement
Creating Custom Rules in FxCop
FxCop lets you create custom rules to enforce your own standards. You use the FxCop SDK to develop custom rules and add them to the in-built default rules. Custom rules are classes that inherit from the BaseIntrospectionRule class in the FxCop SDK. You describe custom rules in an XML document, and then embed that in the application as a resource.

Implementing Custom Rules using the FxCop SDK
I'll provide a more detailed example in the rest of this article, but as an overview, here are the steps you need to follow to create custom rules in FxCop:

First, create a class library project and add references to the FxCopSdk.dll and Microsoft.Cci.dll assemblies.

Next, create a class to implement the rule. This class must inherit from the BaseIntrospectionRule class.

Add the following using statements to your custom rule class:

using Microsoft.Cci; using Microsoft.Tools.FxCop.Sdk; using Microsoft.Tools.FxCop.Sdk.Introspection;

Create a constructor for your custom rule class that looks similar to this:

public MyCustomRule() : base( "MyCustomRule", "ProjectName.RuleCategory.RuleDefinitions", typeof(MyCustomRule).Assembly ) { }

You'll also need to describe the rule in a XML document file and save that as an embedded resource (change the "Build Action" for this file to be "Embedded Resource") in the main assembly. I'll describe that process in more detail below.

Save and compile to build the assembly.

Finally, add your new rule using the FxCop IDE. Note that this assembly gets added to the FxCop rules engine. At this point, you can use FxCop to inspect a target assembly using both the predefined and your new custom rule.

A Custom Rule Example
In this example you'll design a BaseRule class that you can later extend with CustomRule classes. Here are the files you'll require:

  • NamingRules.xml—Contains the description of each and every rule that you will implement.
  • NamingRules.cs—Contains C# code for all the custom naming rules.
  • BaseRule.cs—The C# code file containing the BaseRule class.
Here's the rule definition for the sample custom rule in XML format in the NamingRules.xml file:



<Rule TypeName="ObjectNamingRule" Category="FxCopCustomRules.CustomRules" CheckId="Rule0001"> <Name> Objects of the Object class should be properly named.</Name> <Description> Objects of the Object class should be prefixed with obj'. </Description> <GroupOwner>Joydip</GroupOwner> <DevOwner>Self</DevOwner> <Owner>Self</Owner> <Url>http://localhost</Url> <Resolution>Change the name of the object {0} so that it starts with a prefix of 'obj'.</Resolution> <Email>joydipkanjilal@yahoo.com</Email> <MessageLevel Certainty="50">Warning</MessageLevel> <FixCategories>Breaking</FixCategories> </Rule>

The rule name in the preceding code is InterfaceNamingRule, and the category is NamingRules.

Here's the code for the BaseRule class (which you can find in the BaseRule.cs file in the downloadable code) that you'll extend with a custom rule class shortly.

using System; using Microsoft.Cci; using Microsoft.Tools.FxCop.Sdk; using Microsoft.Tools.FxCop.Sdk.Introspection; [CLSCompliant(false)] namespace FxCopCustomRulesLibrary { public abstract class BaseRule : BaseIntrospectionRule { protected BaseRule(string name) : base(name, "FxCopCustomRulesLibrary.NamingRules.NamingRules", typeof(BaseRule).Assembly) { } } }

The BaseRule class in the preceding code provides some common functionality to facilitate code reusability and simplify code maintenance. For example, the class constructor makes a call to the BaseIntrospectionRule constructor with the required parameters.

Listing 1 shows a custom rule class that inherits from the BaseRule class. You'll find this in the NamingRules.cs file in the downloadable code.

Analyzing assemblies using FxCop
You need to follow this sequence of steps to analyze assemblies using FxCop.

  • Start the FxCop application
  • Select and then add the targets (assemblies) to be analyzed by the project. (Project->Add Targets on the FxCop menu system)
  • Add the Custom rules (if any) to the FxCop Application (Project->Add Rules on the FxCop menu system)
  • Click on the Analyze button
Integrating FxCop with VS.NET
You can integrate FxCop with the Visual Studio IDE to enable developers to analyze the source code at the time of coding. Integration is beneficial for the developers because it FxCop's generated reports help developers find and fix errors before application deployment.

How to integrate?
FxCop ships with a command-line tool named fxcopcmd.exe that you can use to integrate FxCop with the VS.NET IDE or to add FxCop analyses and report generation to your build process. To integrate it:

  • Select Tools—> External Tools from the VS menu; you'll see a popup window with configurable options.
  • In the Title field, enter FxCop.
  • In the Command field enter C:\Program Files\Microsoft FxCop 1.32\FxCopCmd.exe. Note: This is the default installation path. You may need to use a different path if you installed FxCop in a custom location.
  • In the Arguments field, enter /f:$(TargetPath) /r:"C:\Program Files\Microsoft FxCop 1.32\Rules" /c
  • Set the Initial Directory field to $(ProjectDir)
  • Check the "Use Output Window" option.
Save your changes. Now, from the VS.IDE, you can click Tools - > FxCop in the menu system. FxCop loads the rules and displays the report in the VS.NET IDE's Output window.

The software applications in today's IT industry are increasingly becoming more complex and sophisticated with time. Microsoft released the free FxCop code analysis tool to help developers achieve the goal of designing quality software applications that conform to predefined standards. In this article, you've seen how to use FxCop to create custom rules and analyze managed assemblies for compliance to required standards, and how to integrate FxCop into the Visual Studio IDE, making it more convenient for developers to analyze their assemblies for problems on demand, or as part of their standard build process.



Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap