Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Roslyn编译解决方案时出现核心类型错误_C#_.net_Msbuild_Roslyn - Fatal编程技术网

C# 使用Roslyn编译解决方案时出现核心类型错误

C# 使用Roslyn编译解决方案时出现核心类型错误,c#,.net,msbuild,roslyn,C#,.net,Msbuild,Roslyn,我正在尝试使用Roslyn(Microsoft.CodeAnalysis.CSharp 2.8.2和friends)编译一个解决方案,其中包含几个针对.NET Framework 4.6的C#项目 所讨论的解决方案在VS2017中构建得很好,但当我尝试通过Roslyn进行编译时,我收到了大量关于基本核心类型的错误,如“找不到类型或命名空间名称'AssemblyTitleAttribute' 编辑:也许更重要的是,打开解决方案后,MSBuildWorkspace本身会发出诊断。它们看起来像这样:

我正在尝试使用Roslyn(Microsoft.CodeAnalysis.CSharp 2.8.2和friends)编译一个解决方案,其中包含几个针对.NET Framework 4.6的C#项目

所讨论的解决方案在VS2017中构建得很好,但当我尝试通过Roslyn进行编译时,我收到了大量关于基本核心类型的错误,如“找不到类型或命名空间名称'AssemblyTitleAttribute'

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

处理文件时Msbuild失败 “C:\dev\MySolution\My.Assembly.Name\My.Assembly.Name.csproj”和 消息:C:\ProgramFiles(x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1603,5):无法执行“GetReferenceNearestTargetFrameworkTask”任务 从程序集“C:\Program Files(x86)\Microsoft”实例化 视觉的 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:\ProgramFiles(x86)\Microsoft 视觉的 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 */ }
我做错了什么?

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

  • 添加门面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上:

编辑:根据要求,以下是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>

调试
任意CPU
{C768D1C6-3C45-4170-A4DB-B05B7F833181}
图书馆
性质
My.Assembly.Name
My.Assembly.Name
v4.6
512
..\
真的
$(SolutionDir)\StyleCop.ruleset
真的
http://gitlink/{revision}/{filename}
真的
满的
假的
bin\Debug\
调试;痕迹
促使
4.
bin\Debug\My.Assembly.Name.XML
1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592, 1598, 1710, 1711, 1712, 1723
真的
pdbonly
真的
bin\Release\
痕迹
促使
4.
假的
..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll
假的
..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll
..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll
真的
假的
..\packages\RazorEngine.3.4.2\lib\net45\RazorEngine.dll
假的
..\packages\Microsoft.AspNet.Razor.3.1.1\lib\net45\System.Web.Razor.dll
设计师
设计师
{96b3bcc3-9052-403e-97ca-44eef392ee15}
My.OtherProject.Name
此项目引用此计算机上缺少的NuGet包。启用NuGet软件包还原以下载它们。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=322105. 缺少的文件是{0}。

如果您将.proj文件放在。您是否也尝试手动为您的项目创建编译?我的意思是添加所有树、引用并放置特定选项。这个汇编有诊断吗?@GeorgeAlexandria感谢您的评论。我在帖子中添加了一个匿名版本的csproj。此外,正如您所建议的,我尝试通过添加所有.cs文件和元数据引用手动创建编译。这也打破了。然而,我随后从facades目录切换到当前框架安装的目录,这就成功了!好消息是,当您从框架目录添加引用时,它会工作。我看到
MsBuildWorkspace
在解析解决方案时向您提供了诊断。我建议尝试使用与VS相同的nuget版本修复诊断,或者添加绑定重定向。