Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Conditional equivalent of !DEBUG [duplicate]

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()
{
    //...
}
like image 374
grenade Avatar asked Aug 18 '14 10:08

grenade


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

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()
{
    //...
}
like image 151
Gusdor Avatar answered Sep 30 '22 00:09

Gusdor