I have users using both Visual Studio and FxCop, and I want to create custom rules that run on both. Is this possible?

Although in the future we do plan on making it easier to move custom rules between Visual Studio and FxCop, currently you must recompile each rule assembly against the version of FxCop/Visual Studio you want to target.

Typically this involves the (painful) step of manually removing, and re-adding the references to both FxCopSdk.dll and Microsoft.Cci.dll.

To improve this process, we have developed an MSBuild targets files that can be imported by your custom rules projects, which makes switching between targetting FxCop 1.35 and Visual Studio as easy as changing build configurations.

In the attached download, the same sample rules project that was made available here, has been included, however, it has been updated to import this new targets file. This project has 4 build configurations, FxCopDebug, FxCopRelease, VisualStudioDebug and VisualStudioRelease. As you can probably guess from the names, the former two target building against FxCop, while the later two target building against Visual Studio. You can use this project as a starting base, or follow the below steps to import this targets file in your own projects:

  1. Copy Microsoft.CodeAnalysis.CustomRules.targets (attached below) to the same folder that contains your Visual Studio solution
  2. Open your custom rules project and remove any references to FxCopSdk and Microsoft.Cci
  3. Right-click on your project and choose Unload Project
  4. Right-click on your project and choose Edit [ProjectName]
  5. Add the following bolded line at the location indicated (at the bottom of the file):
    [...]
        <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
        <
    Import Project="..\Microsoft.CodeAnalysis.CustomRules.targets" />
    </Project>

    By default, this looks for the targets file one directory above your project file (which typically contains your solution).
  6. At the top of the project file, after the first <PropertyGroup></PropertyGroup> element, but before the second, add the following bolded lines at the location indicated:
    [...]
    </PropertyGroup
    >
    <
    PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'FxCopDebug|AnyCPU' "
    >
        <
    DebugSymbols>true</DebugSymbols
    >
        <
    DebugType>full</DebugType
    >
        <
    Optimize>false</Optimize
    >
        <
    OutputPath>bin\FxCopDebug\</OutputPath
    >
        <
    DefineConstants>DEBUG;TRACE</DefineConstants
    >
        <
    ErrorReport>prompt</ErrorReport
    >
        <
    WarningLevel>4</WarningLevel
    >
        <
    RunCodeAnalysis>true</RunCodeAnalysis
    >
        <
    CodeAnalysisRules></CodeAnalysisRules
    >
        <
    CodeAnalysisBuildTarget>FxCop</CodeAnalysisBuildTarget
    >
    </
    PropertyGroup
    >
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'VisualStudioDebug|AnyCPU' ">
        <
    DebugSymbols>true</DebugSymbols
    >
        <
    DebugType>full</DebugType
    >
        <
    Optimize>false</Optimize
    >
        <
    OutputPath>bin\VisualStudioDebug\</OutputPath
    >
        <
    DefineConstants>DEBUG;TRACE</DefineConstants
    >
        <
    ErrorReport>prompt</ErrorReport
    >
        <
    WarningLevel>4</WarningLevel
    >
        <
    RunCodeAnalysis>true</RunCodeAnalysis
    >
        <
    CodeAnalysisRules></CodeAnalysisRules
    >
        <
    CodeAnalysisBuildTarget>VisualStudio</CodeAnalysisBuildTarget
    >
    </
    PropertyGroup
    >
    <
    PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  7. Right-click on the project and choose Reload Project, answering Yes to both questions that are asked
  8. Right-click on the solution and choose Configuration Manager
  9. In the Active solution configuration dropdown, choose New
  10. In the New Solution Configuration dialog, enter FxCopDebug as the name and click OK
  11. In the Active solution configuration dropdown, choose New
  12. In the New Solution Configuration dialog, enter VisualStudioDebug as the name and click OK

Once you have done the above, switching the active build configuration between FxCopDebug and VisualStudioDebug will now also change the references to FxCopSdk and Microsoft.Cci to point to the associated version.

If you have any issues with the Microsoft.CodeAnalysis.CustomRules.targets file, or with the custom rules themselves, don't hesitate to visit the FxCop forum.