无法实例化构建环境 CodeAnalysis 任务

Build environment CodeAnalysis task could not be instantiated

我正在尝试在我的项目中使用 Microsoft.Build.Evaluation.Project 到 运行 MSBuild。

我遇到了这个错误,我不知道为什么,也不知道从哪里开始诊断它:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets: error MSB4127: The "CodeAnalysis" task could not be isntantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\.\FxCopTask.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.Tasks.CodeAnalysis' to type 'Microsoft.Build.Framework.ITask'.

我不确定如何 "verify the task assembly" 或添加 "a binding redirect for the Microsoft.Build.Framework." 谁能给我指明正确的方向?

这里的答案在于模糊引用。默认 Microsoft.Build 引用是 v4.0.

原来要引用最新版本(至少在 VS 2013 社区中)我必须浏览到 C:\Program Files (x86)\MSBuild.0\bin 中的程序集并手动添加引用。成功!

感谢 BuildManager to use another version of MSBuild 指出这个解决方案。

'Adding a binding redirect' 正在将以下内容添加到您的 app.config 文件中:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="12.0.0.0"/> <!--verify version!-->
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

对我来说,这解决了问题。不过,我的错误是从 The "csc" task could not be instantiated 开始的。