简体   繁体   English

如何在运行时使用众所周知的属性查找正在运行的 MSBuild 版本 - .NET Core 或 .NET Framework

[英]How to find at runtime with well known properties which build of MSBuild is running - .NET Core or .NET Framework

In custom build projects there could be two possible MSBuild frameworks in use.在自定义构建项目中,可能会使用两个可能的 MSBuild 框架。 One built with .NET Core and the Visual Studio MSBuild built with .NET Framework.一个使用 .NET Core 构建,而 Visual Studio MSBuild 使用 .NET Framework 构建。 The latter is run always when building from Visual Studio while the former is used by command line build scripts.后者在从 Visual Studio 构建时始终运行,而前者由命令行构建脚本使用。 Due to the fact, that Visual Studio is our IDE of choice and it supports currently only .NET Framework MSBuild we cannot drop support for it (migration to pure .NET Core MSBuild breaks IntelliSense, refactorings, and multiple other VS goodies).由于事实上,Visual Studio 是我们选择的 IDE,它目前仅支持 .NET Framework MSBuild,我们不能放弃对它的支持(迁移到纯 .NET Core MSBuild 会破坏 IntelliSense、重构和多个其他 VS 优点)。

One of the projects in the code base is a C++ vcxproj which can be built on Windows only with .NET Framework MSBuild while on other platforms it uses make .代码库中的项目之一是 C++ vcxproj ,它只能使用 .NET Framework MSBuild 在 Windows 上构建,而在其他平台上则使用make C++ project is a dependency for some C# projects and should be built when they are building based on standard MSBuild logic. C++ 项目是某些 C# 项目的依赖项,应在基于标准 MSBuild 逻辑构建时构建。

Currently it is included into the C# projects in the following way:目前,它通过以下方式包含在 C# 项目中:

<ProjectReference Condition="$(SolutionFileName) == 'Solution.sln'" Include="nativeproject\nativeproject.vcxproj" />

Condition is always true when projects are building from Visual Studio (VS populates this value) and we make it false when building from the build script by calling MSBuild on the project level and building vcxproj separately.当从 Visual Studio 构建项目时,条件始终为真(VS 填充此值),我们通过在项目级别调用 MSBuild 并单独构建 vcxproj 从构建脚本构建时将其设为假。 However, it does not provide a universal solution that would be based on condition differentiating between .NET Core and .NET Framework MSBuild.但是,它没有提供基于条件区分 .NET Core 和 .NET Framework MSBuild 的通用解决方案。

Unfortunately, the best of all worlds where .NET Core MSBuild would be supported by Visual Studio and would be used for vcxproj build is still far away.不幸的是,Visual Studio 支持 .NET Core MSBuild 并将用于 vcxproj 构建的所有世界中最好的仍然很遥远。

How to find at runtime with well known properties which build of MSBuild is running - .NET Core or .NET Framework.如何在运行时使用众所周知的属性查找正在运行的 MSBuild 版本 - .NET Core 或 .NET Framework。

Try using $(MSBuildRuntimeType) from this document .尝试使用本文档中的$(MSBuildRuntimeType)

My test:我的测试:

For test.csproj in which there's custom target like this:对于test.csproj ,其中有这样的自定义目标:

  <Target Name="Test" AfterTargets="build">
    <Message Text="value is $(MSBuildRuntimeType)" Importance="high"/>
  </Target>

Its value is Full when I use msbuild test.csproj (.net fx msbuild, same result in VS)当我使用msbuild test.csproj (.net fx msbuild,VS 中的结果相同)时,它的值为Full

It's value is Core when I use dotnet build test.csproj (.net core msbuild that contained in .net core SDK)当我使用dotnet build test.csproj (.net core SDK 中包含的 .net core msbuild)时,它的值是Core

So it seems to meet your needs if you're trying to get one property to determine which kind of msbuild is being used.因此,如果您尝试获取一个属性来确定正在使用哪种 msbuild,它似乎可以满足您的需求。 Hope it helps and if I misunderstand anything, feel free to correct me.希望它有所帮助,如果我误解了什么,请随时纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM