Writing Custom Rules for FxCop
What is FxCop ?
FxCop stands for “Framework Police”. It’s a tool designed to check .net code for violations of a wide range of programming rules and conventions based on Microsoft’s .Net Framework design guidelines and other standards. It does not analyze source code, but rather analyzes the MSIL (Microsoft Intermediate language) generated by .NET compilers and persisted in .NET assemblies (EXE and DLL files). It is (thankfully) not coupled to any particular .NET programming language, and will work with all .net compliant languages (VB.Net, C#, C++ etc.).
Features of FxCop
· It works only with assembly metadata but not with source code. It has the following advantages:
· It provides an automated way of identifying code that violates rule.
· It works with any managed assembly created by any .NET-compliant language.
· It has an extensive store of built-in rules.
· It supports to create custom rules.
· It produces a rule violation reports.
· It is a free and open source tool.
· It can be integrated with Visual Studio.
Why Use FxCop?
When we are talking about any product or project three major and most important considerations are cost, time and quality. Every client wants a quality product to be delivered with within budget and also within the deadline. Tool like FxCop have an important role in achieving the abovementioned these goals.
FxCop’s inbuilt rules improve the code in a number of areas including performance, security and globalization/localization and thus help to produce quality product.
FxCop helps developers identify the violations of rules from the very early stage of development and thus saves lot of time for code refactoring which indirectly saves man-hours and cost.
How to get FxCop ?
Anyone can download the latest copy of FxCop (version 1.36) from http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772.
FxCop has its own built-in rules. Considering the latest version (1.36) of FxCop we have the following built in rules (see screen shot I):
· Design Rules
· Globalization Rules
How to Run FxCop?
In the above screen shot we can see that there are basically two tabs: Targets and Rules. Using Add Target (Project->Add Target) we can map any DLL or .exe which we want to analyze code for and using the Add Rule ((Project->Add Rule) option we can add our own custom rules. All rules are displayed in a tree hierarchy where we can select or deselect any particular rule or rules that we do not want to implement. Finally we need to click the Analyze button which will produce a rule violation report .
By clicking on a violation in the report, we can obtain detailed information about that violation. In the screen shot we can see a hyperlink to our Visual Studio source file which assists us in locating the code that needs to be modified (see screen shot below): Here we have two choices:
1. Update the code.
2. Keep the code as it is to update latter.
If we click in the hyperlink of location path we can reach the exact line of code that we have to modify (see screen shot below where the exact line is marked with red color).
In the above screen shot we can update the code marked under the red line as follows to resolve the rule violation:
Resolved code: string strFullName = strName + ” ” + strTitle;
After updating the code , we can run FxCop again and see the issue has been resolved as there is nothing in the report for violating the ToString() related issue(see screen shot below)
Pages: 1 2