Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Programmatically get conditional compilation symbols?

Programmatically get conditional compilation symbols?

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jon Hulatt
    wrote on last edited by
    #1

    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;

    L P 2 Replies Last reply
    0
    • J Jon Hulatt

      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;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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");
      #endif

      will 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

      J 1 Reply Last reply
      0
      • L Lost User

        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");
        #endif

        will 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

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        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;

        1 Reply Last reply
        0
        • J Jon Hulatt

          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;

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups