Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Article

Customising FxCop using Introspection

Rate me:
Please Sign up or sign in to vote.
2.85/5 (5 votes)
1 Nov 2007CPOL2 min read 26K   387   12   1
This article explains how to create some of the advanced FxCop Custom rules.

Introspection vs Reflection

Many static analysis tools use simple source-code inspection to identify issues. However, with FxCop, Microsoft decided to leverage the inherent functionality of .NET itself as the basis for creating rules. A very useful feature of .NET is called reflection. Using reflection, you can programmatically inspect other assemblies, classes, and members. You can even invoke methods or access fields, public or private, given appropriate security settings. Reflection is done without establishing a link to the target assembly at compilation time, a practice known as late binding.

Initial versions of FxCop relied on reflection as the basis for rules. However, a newer option is available, called introspection. Similar to reflection, introspection can inspect a target assembly to discover its types, and details about those types. It can also invoke members of those types. Introspection does this in a much faster manner than reflection and supports multi-threaded operations. Furthermore, introspection does not lock the files under analysis, a problem suffered by previous versions of FxCop that needed to use reflection. Given the clear advantages of introspection over reflection, Microsoft has leveraged introspection with the rules that are shipped with Team System. We'll also use introspection in this section for our custom rule.

Background

As theoretical is very well discussed in the below-mentioned article in the PDF, I am not reinventing the wheel again.

Using the Code

This article contains the code for the below mentioned custom rules. The code uses .NET 2.0 and FxCop V 1.35.

  1. Checking for explicit usage of SQL objects in your application
  2. Class level variable naming convention
  3. Method level variable naming convention
  4. Validating method names
  5. Validating the method parameters
  6. Validating your assembly for valid XML comments
  7. How to track through session variables
  8. Validating Web control naming convention
  9. Check for dead classes in your assembly

Points of Interest

One thing is weird. If you precompile the ASP.NET application and view the IL code of the ASP.NET DLL, the locals declared in the public or private methods in the code behind file will lose their identity (the names of the locals declared will be treated as local0, local1). Happy programming.

History

  • 2nd November, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs it stille possible to validate xml comments? Pin
flodpanter10-Jan-08 22:00
flodpanter10-Jan-08 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.