#if DEBUG always runs, even in release mode

Asked

Viewed 1,911 times

4

I’m trying to use the directive #if for a given method to be executed only after publication (in release mode).

The code is something like

#if !DEBUG
    AlgumMetodo();
#endif

I’ve also tried to add the ConditionalAttribute in the signature of the method

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

The problem is that of the two forms the method never is executed, no matter if it is in mode release or debug.

I already checked the properties of the project, in the tab build. In configuration release the checkbox define DEBUG constant is unchecked and in configuration debug he is marked.

Is there any other configuration I can verify or something I’m doing wrong?

  • Try putting a #define RELEASE at the beginning of this code to see if anything changes, at least we’ll know better where the problem is. I’ve never used this, but I don’t think it’s used like this. I think if it’s to be used in release, then leave normal, only block what should only be used in debug mode.

  • I need just the opposite. I need the method not run if it is in debug. Anyway I managed to resolve by setting RELEASE in the build settings of the project.

  • 1

    It’s a little weird that, I think you’re conceptualizing wrong. I don’t think resources were created to do this. Still you need to do tests to find out where the problem is, as I said.

  • You are Publicando your app or picking up the contents of bin folder? remember that the contents of bin folder will always run in mode Debug since it will always be compiled in this format for you to proceed with the tests.

  • The contents of the folder bin/Release is generated by release mode.

1 answer

6


Instead of resorting to logic reversal (if !DEBUG), set your own symbol in the settings that need it.

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

inserir a descrição da imagem aqui

With the symbol defined, 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 comma-separated values, so if you need to define more symbols you can do RELEASE, METRICS, ETC and use them as demonstrated above.

  • 3

    That’s what I figured.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.