简体   繁体   English

自定义条件编译符号

[英]Custom conditional compilation symbols

I'm currently struggling using custom configurations. 我目前正在努力使用自定义配置。 My solution has one .NET Standard Library and two other Projects (one for Windows, one for Android) which uses the library. 我的解决方案有一个.NET标准库和另外两个使用该库的项目(一个用于Windows,一个用于Android)。

What I try to do is giving the library the compiler constant WINDOWS and MOBILE. 我想做的就是给库提供编译器常量WINDOWS和MOBILE。

Thats how I tried it several times: 那就是我几次尝试的方式:

  1. Create two new configurations WindowsDE and MobileDE, copy settings from Debug configuration and create new project configuration. 创建两个新的配置WindowsDE和MobileDE,从“调试”配置复制设置并创建新的项目配置。 At some trys I also deleted the default Debug configuration but that didn't helped 在一些尝试下,我还删除了默认的Debug配置,但这并没有帮助

  2. Properties of the library -> Build, choose WindowsDE and put WINDOWS into Conditional compilation symbols field then choose MobileDE and put ANDROID in it. 库的属性-> Build,选择WindowsDE并将WINDOWS放入条件编译符号字段中,然后选择MobileDE并将ANDROID放入其中。

I'm testing it with calling a method in the library : 我正在通过调用库中的方法对其进行测试:

#if WINDOWS
        System.Diagnostics.Debug.WriteLine("Windows");
#endif

#if ANDROID
        System.Diagnostics.Debug.WriteLine("Android");
#endif

But that doesn't work at all. 但这根本不起作用。 Also just using 也只是使用

System.Diagnostics.Debug.WriteLine("anything");

without having any #if does not print and at some trys I could not even debug the library anymore. 没有#if不会打印,并且在某些尝试下我什至无法调试该库了。

Would appreciate any help on this 希望对此有任何帮助

You can define conditional compiling constants in the project properties 您可以在项目属性中定义条件编译常量

在此处输入图片说明

#if WINDOWS
    System.Diagnostics.Debug.WriteLine("Windows"); // NOT printed!
#endif

#if ANDROID
    System.Diagnostics.Debug.WriteLine("Android"); // Printed!
#endif

You can enter several symbols separated by semicolons. 您可以输入多个用分号分隔的符号。 Don't set them true or false . 不要将它们设置为truefalse The ones that are listed are true . 列出的是true The missing ones are automatically false . 缺少的自动为false

If neither the one nor the other prints, then possibly the solution does not compile and you are running an old code. 如果一个都不打印,则解决方案可能无法编译,您正在运行旧代码。 Try to rebuild the solution. 尝试重建解决方案。

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

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