How can I make it work?
In C# code:
if (condition)
{
#define CONSTANT
}
I tried doing something through <DefineConstants Condition="'$condition'"></DefineConstants> in a project file’s <PropertyGroup> section.
But it won’t work because the condition will be based on something that we know AFTER the program is built.
>Solution :
No; your runtime code (if) cannot affect compile-time directives (#define). You need to use another approach. Maybe use a static readonly bool that you calculate once at runtime – the JIT on up-to-date .NET versions will usually treat this the same as a const, and perform dead-code removal appropriately during the JIT process, making if (SomeStaticReadOnlyOrConst) almost the same as #if SOME_SYMBOL.