简体   繁体   中英

Preprocessor directive cannot be put in AND in C#

I need to put some code under a preprocessor directive. such directives can be defined in different projects.

My situation is the following:

File Constants.cs (which is in project Proj1Dll.csproj)

#define DEV
... rest of the code

File Page1.cs (which is in project MainProj.csproj, which references Proj1Dll)

#define SHOW_BTN

...

#if (DEV && SHOW_BTN)
  public static void Foo()
  {
    Debug.WriteLine("Both DEV and SHOW_BTN directives are defined");
  }
#endif

Such configuration is not working, in the sense that method Foo() is not part of the compilation.

If I check instead #if (DEBUG && SHOW_BTN) everything is fine.

Is there a way to deal with such directives when they are defined in different projects?

Assuming you are using Visual Studio, you can go to the Project Properties, and under the Build tab, you will see 'Conditional compilation symbols'

Put your synbols in here and they will be recognised project wide .

In your example you would put just DEV if you wanted more than one, separate them with semicolon eg DEV;SHOW_BTN

If you want something to be solution wide and need only one symbol you can use the configuration manager to create a new configuration specifically for this build and call it what you like - this then gives you the same effect. (It's why in your example DEBUG works)

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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