Export (0) Print
Expand All
Expand Minimize

Migrating Custom Rules to Visual Studio Team System 2008

Visual Studio 2008

This article describes some basic guidance for migrating custom code analysis rules written for Visual Studio Team System 2005 or FxCop 1.35 for use with FxCop 1.36 and Visual Studio Team System 2008 Development Edition and Visual Studio Team Suite 2008.

Cc901387.note(en-us,VS.90).gifNote:
Creating and using custom rules is not officially supported by Microsoft. Do so at your own risk.

Several changes were made to the FxCop extensibility model which could cause your custom rules not to build for Visual Studio Team System 2008 and FxCop 1.36.

  • The Microsoft.Cci and Microsoft.FxCop.Sdk.Introspection namespaces were merged into the Microsoft.FxCop.Sdk namespace for Visual Studio Team System 2008 and FxCop 1.36. You must remove all references to Microsoft.Cci and Microsoft.FxCop.Sdk.Introspection in your custom rules code.
  • Many of the helper methods found in RuleUtilties were removed from FxCop 1.36. If you used one of these obsolete methods, you will have to manually code a replacement.

Follow these steps to upgrade and migrate your custom code analysis rules to Visual Studio Team System 2008 and FxCop 1.36.

To migrate custom code analysis rules
  1. Upgrade your project to Visual Studio 2008 by using the Visual Studio Conversion Wizard.

  2. Update the project References in Solution Explorer to point to the latest version of FxCopSdk.dll and Microsoft.Cci.dll found in n:\<installpath>\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\.

  3. Edit your code to remove references to the discontinued namespaces Microsoft.Cci and Microsoft.FxCop.Sdk.Introspection.

  4. Look for RuleUtilities helper methods in your code. If you find obsolete helper methods in your code:

    1. Manually code replacement methods for any helper methods that you used that have been removed from RuleUtilities.
  5. Recompile your custom rules and fix any remaining errors.

  6. Copy the recompiled binaries to n:\<installpath>\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\Rules.

  7. Test that the custom rule works as expected.

Let's look at the sample custom rule, FxCopRuleSamples, that David Kean created for use in Visual Studio 2005 and FxCop 1.35. You can download a copy of the FxCopRuleSamples project from the blog entry FAQ: How do I integrate custom rules with Visual Studio ?.

Upgrade the Project and Update References

After you extract the files, double-click FxCop Rule Samples.sln to upgrade the solution from Visual Studio 2005 to Visual Studio 2008. After you upgrade the solution, you'll see two errors in the Error List window. These errors indicate that you must update the project References to point to the latest versions of FxCopSdk.dll and Microsoft.Cci.dll. In Solution Explorer, expand the References node and remove the outdated references to FxCopSdk.dll and Microsoft.Cci.dll. Then add new references to the latest versions of FxCopSdk.dll and Microsoft.Cci.dll, found at n:\<installpath>\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\, to the project.

Remove Deprecated APIs

Now open BaseRule.css. You'll notice that this file still references Microsoft.Cci and Microsoft.FxCop.Introspection:

using System;

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

namespace Microsoft.FxCop.Rules

Remove using Microsoft.Cci; and using Microsoft.FxCop.Sdk.Introspection;.

Repeat this step for PrivateFieldsShouldHaveCorrectPrefix.cs.

Find and Fix RuleUtilities Helper Methods

When you opened PrivateFieldsShouldHaveCorrectPrefix.cs, you might have noticed errors in the Error List window. Removing using Microsoft.Cci; and using Microsoft.FxCop.Sdk.Introspection; resolved two of the errors. The remaining error relates to a RuleUtilities helper method. Double-clicking the error text takes you to the following section of code:

// Ignore compiler generated event backing stores
     if (RuleUtilities.IsEventBackingField(field))
          return null;
if (!name.StartsWith(FIELD_PREFIX))
     Problems.Add(new Problem(GetNamedResolution("Prefix", name, FIELD_PREFIX), field));

          return Problems;

IsEventBackingField was removed from RuleUtilities for FxCop 1.36 and Visual Studio Team System 2008. We have to update the code to replace the helper method.

if (!name.StartsWith(FIELD_PREFIX, StringComparison.Ordinal))
          Problems.Add(new Problem(GetNamedResolution("Prefix", name, FIELD_PREFIX), field));

          return Problems;

You can now save the changes that were made to the files.

Build the Sample Rule and Copy it to the \FxCop\Rules Folder

Press F5 to build the solution. No errors or warnings are generated; therefore we can copy Microsoft.FxCop.Rules.Samples.dll to n:\<installpath>\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\Rules.

Cc901387.note(en-us,VS.90).gifNote:
Always test newly added rules to verify that the rule catches the errors you intend it to catch.

For more guidance with creating and debugging custom rules, visit the following resources.

Visual Studio Code Analysis and Code Metrics Forum

Visual Studio Code Analysis Team Blog

Community Additions

ADD
Show:
© 2014 Microsoft