Custom Code Analysis in Visual Studio 2010
Microsoft’s .NET code analysis tool, FXCop, has offered the ability to create custom code analysis rules for many years, but the experience has been less than stellar. The version for VS 2010 offers some improvements and a better integration story, but some fundamental problems still remain.
Up front there is something you need to know about .NET code analysis; there is no SDK. Microsoft fully reserves the right to change any aspect of the FXCop API at any time. This means any custom rules you write for VS 2010 may need to be modified or outright rewritten when VS 11 comes out a few years from now. This also means that there is little documentation outside blogs and newsgroups.
There are also no plans to release any of the built-in code analysis rules under a open or shared source license. So all the custom rules you write need to be written from scratch even if a built in one almost does what you want. Since code analysis is a hot topic in Computer Science research and a major selling point for Visual Studio Premium edition, this isn’t likely to change anytime soon.
If you are still interested, the development experience isn’t too bad. Simply inherit from BaseIntrospectionRule, override the methods you care about, and add a matching XML-base meta-data file. Duke Kamstra, Program Manager for Microsoft, has a walk-through on his Code Analysis and Code Metrics blog.
Long-time custom rule writers will note that the procedure has changed significantly from VS 2008. This was done to support the new rule sets feature, which allows one to easily apply and maintain the same rules across multiple projects.
Shane Hastie Aug 30, 2014
Roy Rapoport Aug 28, 2014