如何在解决方案中嵌入 StyleCop?

How do I embed StyleCop in the solution?

我尝试将 StyleCop 集成到 Visual Studio 解决方案中。我希望避免在每个开发人员的每台机器上安装 StyleCop。我多次看到的建议 (example) 是在项目中包含 StyleCop 的二进制文件,将它们存储在版本控制中。

我做到了。它 在我的机器上工作 ,但在未安装 StyleCop 的另一台机器上失败。在我的机器上卸载 StyleCop 后,它在那里也不起作用。

错误信息如下:

Severity Code Description Project File Line Error The "StyleCopTask" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild..\StyleCop 4.7\StyleCop.dll. Could not load file or assembly 'file:///C:\Program Files (x86)\StyleCop 4.7\StyleCop.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. Demo

这是我在每个 .csproj 文件中包含的内容:

<Import Project="$(SolutionDir)\externs\Microsoft.StyleCop\StyleCop.targets" />

目录 C:\demo\externs\Microsoft.StyleCop 包含:

怎么了?

StyleCop.Targets 似乎包含绝对路径:

<UsingTask
    AssemblyFile="$(MSBuildExtensionsPath)\..\StyleCop 4.7\StyleCop.dll"
    TaskName="StyleCopTask"/>

为了能够在未安装该应用程序的计算机上使用 StyleCop,请将此路径更改为类似于:

<UsingTask
    AssemblyFile="$(SolutionDir)\externs\Microsoft.StyleCop\StyleCop.dll"
    TaskName="StyleCopTask"/>