首页 > 解决方案 > 使用 Roslyn 编译解决方案时出现核心类型错误

问题描述

我正在尝试使用 Roslyn(Microsoft.CodeAnalysis.CSharp 2.8.2 和朋友)编译一个针对 .NET Framework 4.6 的几个 C# 项目的解决方案。

有问题的解决方案在 VS2017 中构建良好,但是当我尝试通过 Roslyn 进行编译时,我收到大量关于基本核心类型的错误,例如“找不到类型或命名空间名称‘AssemblyTitleAttribute’”。

编辑:也许更重要的是,一旦打开解决方案,MSBuildWorkspace 本身就会发出诊断信息。它们看起来像这样:

处理文件“C:\dev\MySolution\My.Assembly.Name\My.Assembly.Name.csproj”时,Msbuild 失败,消息为:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\ 15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1603, 5): "GetReferenceNearestTargetFrameworkTask" 任务无法从程序集 "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\ IDE\CommonExtensions\Microsoft\NuGet\NuGet.Build.Tasks.dll”。请验证任务程序集是使用与您计算机上安装的相同版本的 Microsoft.Build.Framework 程序集构建的,并且您的主机应用程序没有缺少 Microsoft.Build.Framework 的绑定重定向。无法转换类型为“NuGet.Build.Tasks”的对象。GetReferenceNearestTargetFrameworkTask' 以键入 'Microsoft.Build.Framework.ITask'。C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1603, 5):“GetReferenceNearestTargetFrameworkTask”任务已声明或使用不正确,或施工过程中失败。检查任务名称和程序集名称的拼写。

这是我正在使用的代码:

var solution = await MSBuildWorkspace.Create().OpenSolutionAsync(@"C:\path\to\solution.sln");
var project = solution.Products.Single(p => p.Name == "ProjectName");
var compilation = await project.GetCompilationAsync();
var diagnostics = compilation.GetDiagnostics()
    .Where(d => !d.IsSuppressed && d.Severity == DiagnosticSeverity.Error)
    .ToArray();
foreach (var diagnostic in diagnostics) { /* print */ }

我究竟做错了什么?

到目前为止我尝试过的事情:

也发布在 Roslyn 的 GitHub 上:https ://github.com/dotnet/roslyn/issues/29170

编辑:根据要求,这里是 csproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\packages\GitLink.3.1.0\build\GitLink.props" Condition="Exists('..\packages\GitLink.3.1.0\build\GitLink.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{C768D1C6-3C45-4170-A4DB-B05B7F833181}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>My.Assembly.Name</RootNamespace>
    <AssemblyName>My.Assembly.Name</AssemblyName>
    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
    <RestorePackages>true</RestorePackages>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
    <TargetFrameworkProfile />
    <CodeAnalysisRuleSet>$(SolutionDir)\StyleCop.ruleset</CodeAnalysisRuleSet>
    <GitLinkEnabled>true</GitLinkEnabled>
    <GitLinkGitRemoteUrl>http://gitlink/{revision}/{filename}</GitLinkGitRemoteUrl>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <DocumentationFile>bin\Debug\My.Assembly.Name.XML</DocumentationFile>
    <NoWarn>1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592, 1598, 1710, 1711, 1712, 1723</NoWarn>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
    </Reference>
    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
    </Reference>
    <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="RazorEngine, Version=3.5.0.0, Culture=neutral, PublicKeyToken=9ee697374c7e744a, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\RazorEngine.3.4.2\lib\net45\RazorEngine.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data.Entity.Design" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\Microsoft.AspNet.Razor.3.1.1\lib\net45\System.Web.Razor.dll</HintPath>
    </Reference>
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <!-- long list of files -->
    <Compile Include="File1.cs" />
    <Compile Include="File2.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config">
      <SubType>Designer</SubType>
    </None>
    <None Include="packages.config">
      <SubType>Designer</SubType>
    </None>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\My.OtherProject.Name\My.OtherProject.Name.csproj">
      <Project>{96b3bcc3-9052-403e-97ca-44eef392ee15}</Project>
      <Name>My.OtherProject.Name</Name>
    </ProjectReference>
  </ItemGroup>
  <ItemGroup>
    <Analyzer Include="..\packages\StyleCop.Analyzers.1.1.0-beta007\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
    <Analyzer Include="..\packages\StyleCop.Analyzers.1.1.0-beta007\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\packages\GitLink.3.1.0\build\GitLink.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitLink.3.1.0\build\GitLink.props'))" />
    <Error Condition="!Exists('..\packages\GitLink.3.1.0\build\GitLink.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitLink.3.1.0\build\GitLink.targets'))" />
  </Target>
  <Import Project="..\packages\GitLink.3.1.0\build\GitLink.targets" Condition="Exists('..\packages\GitLink.3.1.0\build\GitLink.targets')" />
</Project>

标签: c#.netmsbuildroslyn

解决方案


推荐阅读