[英]Can I determine the .NET SDK version used when building from a .csproj or .targets file?
This is mainly to try to work around an annoying coupling of the.Net analyzers to the version of Visual Studio that the .NET SDK version has an affinity to.这主要是为了尝试解决 .Net 分析器与 .NET SDK 版本具有亲和力的 Visual Studio 版本的恼人耦合。
If I use the MSBuild under my Visual Studio 2019 v16.9.X installation (the LTS version that we are currently using), and it tries to build my csproj with the 5.0.404 .NET SDK (because I also have that SDK installed), I will get a lot of errors, because the analyzers in that version of the SDK want to load VS 16.11.X versions of assemblies that are already loaded by the 16.9.X compilers. If I use the MSBuild under my Visual Studio 2019 v16.9.X installation (the LTS version that we are currently using), and it tries to build my csproj with the 5.0.404 .NET SDK (because I also have that SDK installed) ,我会得到很多错误,因为该版本的 SDK 中的分析器想要加载 16.9.X 编译器已经加载的 VS 16.11.X 版本的程序集。
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..
[...]
I would like to specify the compiler versions to use explicitly, depending on the .NET SDK version that is used to build.我想指定要显式使用的编译器版本,具体取决于用于构建的 .NET SDK 版本。
For instance, if it is in the 5.0.2XX feature band, I would <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.9.0" />
, but if it's in the 5.0.4XX band, I would <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.11.0" />
.例如,如果它在 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" />
。
To do this, I need to be able to tell which SDK version is being used.为此,我需要能够判断正在使用哪个 SDK 版本。
A good place to start looking for your answer is to run msbuild /bl
on your csproj.开始寻找答案的一个好地方是在您的 csproj 上运行msbuild /bl
。
That will create a msbuild.binlog
file which can be opened with Microsoft's MSBuild Log Viewer .这将创建一个msbuild.binlog
文件,可以使用 Microsoft 的MSBuild 日志查看器打开该文件。
From the log viewer, you can easily look at all the properties and their values visible to your project during build.从日志查看器中,您可以轻松查看构建期间项目可见的所有属性及其值。
Once you've found a proper match, you can set a Condition on each PackageReference item.找到合适的匹配项后,您可以在每个 PackageReference 项上设置条件。
Just be mindful that it might screw-up with Inteli-Sense when inside Visual Studio, so maybe have one of the PackageReferences as default, just in case.请注意,在 Visual Studio 中它可能会与 Inteli-Sense 发生冲突,因此可能将 PackageReferences 之一作为默认值,以防万一。
Example: The name of the property you're looking for is MyProperty
示例:您要查找的属性名称是MyProperty
Then:然后:
<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" />
As you figured it out, the NETCoreSdkVersion
property contains the .NET SDK version.如您所见, NETCoreSdkVersion
属性包含 .NET SDK 版本。
And here's an example to use it in combination with the [MSBuild]::VersionGreaterThanOrEquals()
function.下面是一个将它与[MSBuild]::VersionGreaterThanOrEquals()
function 结合使用的示例。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.