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



这主要是为了解决.Net分析器与.Net SDK版本与.有关联的Visual Studio版本之间令人讨厌的耦合问题

如果我在Visual Studio 2019 v16.9.X安装(我们目前使用的LTS版本(下使用MSBuild,并且它试图使用5.0.404.NET SDK构建我的csproj(因为我也安装了该SDK(,我会遇到很多错误,因为该版本的SDK中的分析器希望加载16.9.X编译器已经加载的程序集的VS 16.11.X版本。

30>C:Program Files (x86)Microsoft Visual Studio2019ProfessionalMSBuildCurrentBinRoslyncsc.exe [...]
30>CSC: Error CS8032 : An instance of analyzer Microsoft.CodeAnalysis.UseExplicitTupleName.UseExplicitTupleNameDiagnosticAnalyzer cannot be created from C:Program Filesdotnetsdk5.0.404SdksMicrosoft.NET.SdkcodestylecsMicrosoft.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 Filesdotnetsdk5.0.404SdksMicrosoft.NET.SdkcodestylecsMicrosoft.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 Filesdotnetsdk5.0.404SdksMicrosoft.NET.SdkcodestylecsMicrosoft.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 Filesdotnetsdk5.0.404SdksMicrosoft.NET.SdkcodestylecsMicrosoft.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.00.4XX功能带,我会使用<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.11.0" />

要做到这一点,我需要能够说出正在使用的SDK版本。

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

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

<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>

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

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

在日志查看器中,您可以很容易地查看构建过程中项目可见的所有属性及其值。

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

请注意,在Visual Studio中使用Inteli Sense时,它可能会出错,所以可能会将其中一个PackageReferences作为默认值,以防万一。

示例:您要查找的房产名称为MyProperty

然后:

<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" />

最新更新