#if DEBUG always runs, even in release mode

4

I'm trying to use the #if directive so that a given method runs only after it is published (in release mode).

The code is something like

#if !DEBUG
    AlgumMetodo();
#endif

I've also tried to add ConditionalAttribute to the method signature

[Conditional("RELEASE")]
private void AlgumMetodo() { ... }

The problem is that in both ways the never method runs, it does not matter whether it's in release or debug mode. >

I have checked the project properties in the build tab. In the release configuration the checkbox define DEBUG constant is unchecked and in the debug configuration it is checked.

Are there any other settings that I can check or something I am doing wrong?

    
asked by anonymous 27.01.2016 / 13:11

1 answer

6

Instead of invoking logic inversion ( if !DEBUG ), set your own symbol in the settings that require it.

To do this, access the project properties. In the project properties click on the Build tab. Inside the tab, select the setting you want to change (in this case Release ) and the Conditional compilation symbols box, type RELEASE . See the following image with the final result.

With the symbol set, you can do the following in the code:

public void oMeuMetodo()
{

#if RELEASE

 // o meu codigo que so vai ser executado nas configurações que tenham o simbolo RELEASE definido

#endif

}

Note:

The field accepts values separated by commas, so if you need to define more symbols you can RELEASE, METRICS, ETC and use them as shown above.

    
27.01.2016 / 13:59