Search code examples
c#msbuildcsc

Get compiletime constants in C#


In C# you can define compile-time constants that may be checked to configure compilation

#define MY_CONST
#if MY_CONST
    ...
#else
    ...
#endif

But I can't find a way to see which constants are defined at current line. I need something like #warning DEFINED_CONSTANTS that will give me DEBUG; NET5_0


Solution

  • After some time I've found that it is possible to show defined constants through MSBuild with

      <Target Name="ShowConstants" AfterTargets="AfterBuild">
        <Warning Text="$(DefineConstants)" />
      </Target>
    

    which gives TRACE;DEBUG;NET;NET5_0;NETCOREAPP for net5.0 build. It doesn't show constants for given line, but at least show constants for current configuration.