How to define a constant globally in C# (like DEBUG)

前端 未结 4 805
野性不改
野性不改 2020-12-18 20:13

I want to compile a project differently, according to a constant defined by #define, like this:

#define USE_COMPONENT_X

#if USE_COMPONENT_X
...

#endif


        
相关标签:
4条回答
  • 2020-12-18 20:47

    You may want to go a step further and create different project configurations as variants of the standard Debug and Release project configuration. The Configuration Manager under the build menu will let you accomplish this. Then while you are in the project properties' Build tab you can select the various configurations and set the conditional compilation constants that are appropriate for each configuration. This will save you lots of time when you want to swap back and forth between various permutations of your conditionally compiled code.

    0 讨论(0)
  • 2020-12-18 20:54

    You can add the /define compiler switch.

    1. Open the project's Property Pages dialog box.
    2. Click the Configuration Properties folder.
    3. Click the Build property page.
    4. Modify the Conditional Compilation Constants property.
    0 讨论(0)
  • 2020-12-18 20:58

    Set it in your IDE or use the the compiler command line switch i.e. -define for Mono.

    0 讨论(0)
  • 2020-12-18 21:04

    Hopefully I'm not way off topic, but rather than a "constant" perhaps define an interface for the constant's type and then use one of the many dependency injection frameworks to manage the definition, injection and lifetime of your "constant".

    0 讨论(0)
提交回复
热议问题