繁体   English   中英

如何在 C# /.Net.Core 中定义基于平台(Linux vs Windows)的编译时间常数?

[英]How to define compile time constants based on platform (Linux vs Windows) in C# / .Net.Core?

受此博客的启发,我尝试这样实现:

<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行,项目就可以完美加载。

MSBuild 错误

此方法仅适用于 MSBuild 的 15.0(或更高版本)版本。 由于 mono 现在不附带此版本的 MSBuild。

这里有一个未解决的问题

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

暂无
暂无

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

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