简体   繁体   English

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

[英]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.

相关问题 如何从.csproj文件中确定MVC项目 - How to determine MVC project from .csproj file PowerShell-从.csproj文件获取版本 - PowerShell - Get Version from .csproj file 当我使用管道在azure devop上构建csproj文件时,为什么没有创建bin和obj文件夹? - Why are bin and obj folders not getting created when I am building a csproj file on azure devops using pipeline? 生成时,.NET Core csproj PublishProfileName具有一个值 - .NET Core csproj PublishProfileName has a value when building 我可以在构建.net程序集时指定模块版本ID(MVID)吗? - Can i specify the module version id (MVID) when building a .net assembly? 我可以同时在csproj文件中定位.net standard2.0和net471吗? - Can I target both .net standard2.0 and net471 in csproj file 如何确定给定 .NET 程序集使用的子系统? - How can I determine the subsystem used by a given .NET assembly? 我如何在C#中的csproj文件中读取dll版本 - How do i read dll version in csproj file in C# 是否可以将ReferencePath信息从.vbproj.user(或.csproj.user)文件移动到相应的项目文件中? - Can I move ReferencePath information from a .vbproj.user (or .csproj.user) file into the corresponding project file? 在读取(使用)Kafka 主题时,您可以确定生成消息时使用的模式版本/ID - When reading (consuming) a Kafka topics can you determine the schema version/id used when the message was produced
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM