首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以确定从.NET或.targets文件构建时使用的.targets SDK版本吗?

我可以确定从.NET或.targets文件构建时使用的.targets SDK版本吗?
EN

Stack Overflow用户
提问于 2022-01-06 03:29:23
回答 2查看 674关注 0票数 1

这主要是为了解决.Net分析器与Visual版本之间的一个恼人的耦合,.NET SDK版本与Visual版本有关联。

如果我在VisualStudio2019 v16.9.X安装下使用MSBuild (我们目前正在使用的LTS版本),并且尝试用5.0.404 .NET SDK构建csproj (因为我也安装了这个.NET),我会收到很多错误,因为该版本的SDK中的分析器希望加载16.11.X版本的程序集,这些版本已经被16.9.X编译器加载了。

代码语言:javascript
复制
30>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\csc.exe [...]
30>CSC: Error CS8032 : An instance of analyzer Microsoft.CodeAnalysis.UseExplicitTupleName.UseExplicitTupleNameDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\5.0.404\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CodeStyle.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
30>CSC: Error CS8032 : An instance of analyzer Microsoft.CodeAnalysis.UseSystemHashCode.UseSystemHashCodeDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\5.0.404\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CodeStyle.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
30>CSC: Error CS8032 : An instance of analyzer Microsoft.CodeAnalysis.MakeFieldReadonly.MakeFieldReadonlyDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\5.0.404\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CodeStyle.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
30>CSC: Error CS8032 : An instance of analyzer Microsoft.CodeAnalysis.CSharp.UseNullPropagation.CSharpUseNullPropagationDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\5.0.404\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
[...]

根据用于构建的.NET SDK版本,我希望指定要显式使用的编译器版本。

例如,如果它位于5.0.2XX特性带中,我将使用<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.9.0" />,但如果它位于5.0.4XX波段,我将使用<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.11.0" />

要做到这一点,我需要能够知道使用的是哪个SDK版本。

EN

回答 2

Stack Overflow用户

发布于 2022-04-15 12:26:58

正如您所理解的那样,NETCoreSdkVersion属性包含.NET SDK版本。

下面是一个将其与[MSBuild]::VersionGreaterThanOrEquals()函数结合使用的示例。

代码语言:javascript
复制
<PropertyGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '6.0'))">
  <UseCurrentRuntimeIdentifier>true</UseCurrentRuntimeIdentifier>
</PropertyGroup>

<PropertyGroup Condition="!$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '6.0'))">
  <RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux-x64</RuntimeIdentifier>
  <RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('OSX'))">osx-x64</RuntimeIdentifier>
  <RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Windows'))">win-x64</RuntimeIdentifier>
</PropertyGroup>
票数 1
EN

Stack Overflow用户

发布于 2022-01-06 03:41:25

开始寻找答案的一个好地方是在csproj上运行msbuild /bl

这将创建一个msbuild.binlog文件,该文件可以用微软的MSBuild日志查看器打开。

在日志查看器中,您可以轻松地查看生成期间对项目可见的所有属性及其值。

一旦找到合适的匹配项,就可以为每个PackageReference项设置一个条件。

只是要注意,在Visual内部,它可能会将Inteli感知功能搞砸,因此可能会有一个PackageReferences作为缺省值,以防万一。

示例:您要查找的属性的名称是MyProperty

然后:

代码语言:javascript
复制
<PackageReference Condition="'$(MyProperty)'.StartsWith('5.0.2')" Include="Microsoft.Net.Compilers.Toolset" Version="3.9.0" />
<PackageReference Condition="'$(MyProperty)'.StartsWith('5.0.4')" Include="Microsoft.Net.Compilers.Toolset" Version="3.11.0" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70602117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档