我正在尝试使用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):无法从程序集“C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Professional \ Common7中实例化”GetReferenceNearestTargetFrameworkTask“任务\ IDE \ CommonExtensions \微软\的NuGet \ NuGet.Build.Tasks.dll” . 请验证是否使用与计算机上安装的Microsoft.Build.Framework程序集相同的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 */ }

What am I doing wrong?

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

  • 添加外观DLL元数据引用:
var metadataReferenceDlls = Directory.GetFiles(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\", "*.dll");
    var metadataReferences = metadataReferenceDlls.Select(f => MetadataReference.CreateFromFile(f)).ToArray();
    foreach (var projectId in solution.ProjectIds.ToArray())
    {
        solution = solution.AddMetadataReferences(projectId, metadataReferences);
    }
  • 添加当前运行时元数据引用(与上面相同,但使用 Path.GetDirectoryName(typeof(object).Assembly.Location) 作为搜索路径) . 这个作品!但是,它感觉不理想,因为我仍然需要手动解析所有引用的DLL .

  • 安装Microsoft.Build.Locator并提前调用 MSBuildLocator.RegisterDefaults();

同时发布在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>