5

What is the C# System.Diagnostics.Conditional equivalent of #if (!DEBUG)?

I want to encrypt a section of the app.config file of a console application if it has not been compiled in DEBUG mode. This is achieved like so:

public static void Main(string[] args)
{
    #if (!DEBUG)
    ConfigEncryption.EncryptAppSettings();
    #endif
    //...
}

but somehow, I prefer decorating the encrypt method with a conditional attribute:

[Conditional("!DEBUG")]
internal static void EncryptAppSettings()
{
    //...
}

however this makes the compiler sad: The argument to the 'System.Diagnostics.ConditionalAttribute' attribute must be a valid identifier...

What is the correct syntax for negating the Conditional argument?

EDIT: Thanks to @Gusdor, I used this (I preferred to keep the Program.cs file free of if/else debug logic):

#if !DEBUG
#define ENCRYPT_CONFIG
#endif

[Conditional("ENCRYPT_CONFIG")]
internal static void EncryptAppSettings()
{
    //...
}
grenade
  • 31,451
  • 23
  • 97
  • 126
  • Thanks for pointing out the duplicate folks. I had failed to find it. Sadly the accepted answer over there was "it can't be done" whereas, here I managed to get a solution. :) – grenade Aug 18 '14 at 10:55

2 Answers2

8

Using the attribute will be a bit of a hack but it can be done.

#if DEBUG
//you have nothing to do here but c# requires it
#else
#define NOT_DEBUG //define a symbol specifying a non debug environment
#endif

[Conditional("NOT_DEBUG")]
internal static void EncryptAppSettings()
{
    //...
}
Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • That is quite a big thing to ask of the calling code; it'll work when you control everything, but I'm not sure it works well in the general case – Marc Gravell Aug 18 '14 at 10:47
  • Thanks! I based my implementation on your idea (just dropped the `else` logic in favour of a negation). – grenade Aug 18 '14 at 10:48
  • @MarcGravell if i run up a new stack would you be able to illustrate some cases in which it will fail? – Gusdor Aug 18 '14 at 11:07
  • 3
    My point is that in a *library* scenario; `[Conditional(...)]` is part of the library. If is not obvious to the consumer of a library that they would need to start adding compiler symbols to get the library to behave correctly. – Marc Gravell Aug 18 '14 at 11:09
  • Thanks for clarifying that @Marc. I failed to see the downside earlier, but you're very right, the conditional hides the switch behind a magic symbol, which wouldn't be nice in a lib. – grenade Aug 18 '14 at 11:16
  • 1
    why not just ? #if !DEBUG #define NOT_DEBUG //define a symbol specifying a non debug environment #endif ` – xumix Dec 14 '18 at 17:20
0
#if DEBUG
    // do nothing
#else
   //your code here
#endif
Robert
  • 3,276
  • 1
  • 17
  • 26
  • The problem with this is that during normal development, anything in the #else block can stop compiling (eg. after refactoring) and you won't be any the wiser until you next build in release mode. https://stackoverflow.com/questions/25361149/c-sharp-conditional-equivalent-of-debug – Dan Mar 09 '20 at 11:54
  • @Dan that's more about how well CI/CD pipeline works than about the solution itself – Robert Mar 09 '20 at 15:29
  • I didn't mean CI/CD pipelines. I'm talking about the compiler. If you build your application in debug mode, then the compiler won't attempt to compile anything in the `#else` block, and vice-versa. That means if elsewhere in code, you're refactored something that breaks something in the `#else` block, you won't know about it when building in debug mode. – Dan Mar 16 '20 at 19:11