#if preprocessor directive for directives other than DEBUG

前端 未结 4 1994
暖寄归人
暖寄归人 2020-12-30 19:13

I know that I can use preprocessor directives to check for Debug/Release by doing this:

#if DEBUG
    //debug mode
#elif
    //release mode
#endif

相关标签:
4条回答
  • 2020-12-30 19:41

    It's the same as for DEBUG, assuming that you've defined a build configuration that lists TEST in the "Conditional compilation symbols" text box (under project properties > Build tab; this is a space-delimited list).

    For code that you only want to run in the TEST build configuration:

    #if TEST
    // ...
    #endif
    

    And for code you don't want to run in the TEST build configuration, you can either #else the above, or do this:

    #if !TEST
    // ...
    #endif
    
    0 讨论(0)
  • 2020-12-30 19:45

    There are a couple ways to handle your factorings. In my world, we used four primary techniques:

    1. compiler flags (#if)
    2. partial classes
    3. separate implementations
    4. Runtime decisions

    So for example, we have build configurations for, C# with unmanaged code, C# with all managed code, C# for silverlight. In the C# unmanaged project we have a compile-time symbol UNMANAGED, for C# we have MANAGED and for the silverlight we have SILVERLIGHT. This lets me inject small tasks into the code and share the same files across all projects. No big deal.

    For partial classes, we have separate .cs files for each project that have implementations of the fringe code. This gets used in the cases where we couldn't make this work by having an abstract class as the parent class with most of the implementation and then the fringe code in concrete classes for each target. This works well enough.

    For separate implementations, we acknowledge that there is little that can be shared between the code bases and we're better off with separate code. This is not ideal, but so be it.

    For runtime checks, it's exactly that. Rather than check for DEBUG in a #if, you use a runtime check for a setting to make that choice. Unless you've got heinously huge debug scaffolding, this is not a bad choice as it also lets you do field debugging (but you may have delivery constraints that prevent it).

    Personally, I try to avoid the compiler flags. They make the code harder to read. Honestly, though, there are times where they make sense. We've had classes that wouldn't compile in silverlight just because of the class declaration (I think it was ObservableCollection that wasn't available) and we had to inherit from something else. Everything else worked fine.

    0 讨论(0)
  • 2020-12-30 19:57

    Right click on the Project [Project name] name you want to use the custom precompiler directive.

    Go to the properties item and then to the build tab.

    then you need to add your custom directive there in the textbox. E.g i have added 'Local' as my custom directive see image below

    enter image description here

    Now you can can use the new compiler directive as shown below in your (in C#)

      #if **Local**
        //TODO:Add your c# code here
      #endif
    
    0 讨论(0)
  • 2020-12-30 19:59

    Simple answer is

    • Go to Project->[Project name] Properties->Build
    • Set checked [] Define DEBUG

    Now you can play with DEBUG predecessor directive like

    #if DEBUG
    ...
    #else
    ...
    #endif
    
    0 讨论(0)
提交回复
热议问题