Programmatically get conditional compilation symbols?
-
I use conditional compilation symbols to enable and disable client cusomised parts of my code. Is there a built-in way to determine at run time what symbols were defined at build time?
using System.Beer;
-
I use conditional compilation symbols to enable and disable client cusomised parts of my code. Is there a built-in way to determine at run time what symbols were defined at build time?
using System.Beer;
Take five minutes and think again about your question, I think you already gave yourself the answer ;) The answer is "No". The IL does not contain any references to the code not compiled by the preprocessor directives. Code like this:
#if DEBUG
Console.WriteLine("Debug");
#else
Console.WriteLine("Release");
#endifwill lead to following IL code:
L\_0000: nop L\_0001: ldstr "Debug" L\_0006: call void \[mscorlib\]System.Console::WriteLine(string) L\_000b: nop
if compiled in debug mode, otherwise "Release". regards
-
Take five minutes and think again about your question, I think you already gave yourself the answer ;) The answer is "No". The IL does not contain any references to the code not compiled by the preprocessor directives. Code like this:
#if DEBUG
Console.WriteLine("Debug");
#else
Console.WriteLine("Release");
#endifwill lead to following IL code:
L\_0000: nop L\_0001: ldstr "Debug" L\_0006: call void \[mscorlib\]System.Console::WriteLine(string) L\_000b: nop
if compiled in debug mode, otherwise "Release". regards
Sure, I realised I could engineer code to produce exactly that, but I wondered if it might have been built-in. Perhaps in the assembly meta data, and reflection would be able to find it. But I guess not. thanks anyway
using System.Beer;
-
I use conditional compilation symbols to enable and disable client cusomised parts of my code. Is there a built-in way to determine at run time what symbols were defined at build time?
using System.Beer;
Certainly not automatically, but if it's important enough you could probably engineer a mechanism. Something along these lines comes to mind:
public static readonly System.Collections.Generic.HashSet<string> DefinedOptions =
new System.Collections.Generic.HashSet<string>() ;
...if DEBUG
DefinedOptions.Add ( "DEBUG" ) ;
...
endif
But that would only work where such statements are valid. On the other hand, I question whether or not using conditional compilation is an appropriate technique for your requirement.