简体   繁体   English

os的Visual Studio条件编译

[英]Visual studio conditional compilation for os

I know that there is a way to conditionally compile for target frameworks eg #if net461 ....#elif .... But is there a way to conditionally compile for specific os Like target _os_MAC or target_os_win我知道有一种方法可以为目标框架有条件地编译,例如 #if net461 ....#elif .... 但是有没有一种方法可以为特定的 os 有条件地编译,例如 target _os_MAC 或 target_os_win

If there is can someone guide me to the documentations or tutorials to how to implement it?如果有人可以指导我查看如何实现它的文档或教程?

Part 2: Also , is there a way to create a custom tag so that I do not have to change every tag whenever there is a change to the new target os or framework .eg from net461 to net471第 2 部分:另外,有没有办法创建自定义标签,这样每当新目标操作系统或框架发生更改时,我就不必更改每个标签。例如,从 net461 到 net471

It's an old question, but if someone comes here now, there's a better option.这是一个老问题,但如果现在有人来这里,还有更好的选择。

You do not need to have different configurations and select manually which configuration to use.您不需要有不同的配置并手动选择要使用的配置。

You can use System.Runtime.InteropServices.RuntimeInformation.您可以使用 System.Runtime.InteropServices.RuntimeInformation。 https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation?view=netframework-4.8

There's a good manual here: https://blog.magnusmontin.net/2018/11/05/platform-conditional-compilation-in-net-core/ Minimal info from the link: Change your .csproj file这里有一个很好的手册: https ://blog.magnusmontin.net/2018/11/05/platform-conditional-compilation-in-net-core/ 链接中的最少信息:更改您的 .csproj 文件

<PropertyGroup> 
 <OutputType>Exe</OutputType> 
 <TargetFramework>netcoreapp2.0</TargetFramework> 
 <IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows> 
 <IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux> 
</PropertyGroup>
<PropertyGroup Condition="'$(IsWindows)'=='true'">
 <DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsLinux)'=='true'">
 <DefineConstants>Linux</DefineConstants>
</PropertyGroup>

This will conditionally define constants.这将有条件地定义常量。 Which you later use like so:你以后会像这样使用:

#if Linux
    Console.WriteLine("Built on Linux!"); 
#elif Windows
    Console.WriteLine("Built in Windows!"); 
#endif

This answer assumes you're asking about custom pre-processor symbols (that is how I have interpreted it - do correct me if I am wrong.这个答案假设您在询问自定义预处理器符号(这就是我的解释方式 - 如果我错了,请纠正我。

You could use a custom build configuration:您可以使用自定义构建配置:

Start by going into the build Configuration Manager..首先进入构建配置管理器..

配置管理器

Next, create a new build configuration.接下来,创建一个新的构建配置。 You can copy the configuration from an existing one:您可以从现有配置复制配置:

创建新配置

Then, right click on your Project and go to Properties.然后,右键单击您的项目并转到属性。 Under the build tab, define a conditional compilation symbol:在 build 选项卡下,定义一个条件编译符号:

条件编译符号

Do the same for Windows.对 Windows 执行相同操作。

Then you can write conditional steps like the below:然后您可以编写如下条件步骤:

    class Program
{
    static void Main(string[] args)
    {
#if MACOS
        Console.WriteLine("OSX");
#elif WINDOWS
        Console.WriteLine("Windows");
#endif

        Console.Read();
    }

Depending on your chosen build configuration .. you'll either get:根据您选择的构建配置..您将获得:

OSX:操作系统:

操作系统

Windows:窗户:

窗户

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

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