【问题标题】:How to define compile time constants based on platform (Linux vs Windows) in C# / .Net.Core?如何在 C# / .Net.Core 中定义基于平台(Linux 与 Windows)的编译时间常数?
【发布时间】:2019-11-15 09:26:57
【问题描述】:

blog 的启发,我尝试这样实现:

<DefineConstants Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">Windows</DefineConstants>
<DefineConstants Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">POSIX</DefineConstants>

但我收到此错误:

The function "IsOSPlatform" on type "System.Runtime.InteropServices.RuntimeInformation" is not available for execution as an MSBuild property function

有没有办法解决这个问题?没有这两条 DefineConstants 行,项目就可以完美加载。

【问题讨论】:

  • 你为什么不按照博客的实际建议使用 设置?
  • 我累了,它给了我同样的错误。问题是System.Runtime.InteropServices.RuntimeInformation 不可用
  • 看起来您正在使用旧版本的 MSBuild 进行构建。您使用的是哪个环境?当前的 msbuild 和工具版本是 16
  • 我正在使用mono。也许与它有关?
  • 哪个版本的单声道?

标签: c# linux .net-core csproj conditional-compilation


【解决方案1】:

此方法仅适用于 15.0(或更高)版本的 MSBuild。由于此版本的 MSBuild 目前不提供单声道。

这个here有一个未解决的问题

Visual Studio 可以在 .csproj 文件中编译代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2012-04-16
    相关资源
    最近更新 更多