I have a CSPROJ in which I define my own constant / compiler directive.
<PropertyGroup>
<DefineConstants>$(DefineConstants);MY_DIRECTIVE</DefineConstants>
<MY_DIRECTIVE>true</MY_DIRECTIVE>
</PropertyGroup>
Then I proceed to include its use somewhere in my application, like this:
#if MY_DIRECTIVE
System.Console.WriteLine("Yes");
#else
System.Console.WriteLine("No");
#endif
Picture:
That works fine after building and running the application, but Visual Studio has both lines ( Yes
and No
) highlighted, so if I were to do something like <MY_DIRECTIVE>false</MY_DIRECTIVE>
, Visual Studio should "hide" and grey out and ignore the line where it says System.Console.WriteLine("Yes");
, shouldn't it? I would expect it to, but it doesn't. Am I doing something wrong?
This is not going to work because the MSBuild property ( <MY_DIRECTIVE>true</MY_DIRECTIVE>
) has nothing to do with the C# preprocessor directive ( <DefineConstants>$(DefineConstants);MY_DIRECTIVE</DefineConstants>
) - these are two independent concepts.
If you want to trigger the #else
branch you have to bring these concepts together, eg:
<PropertyGroup>
<MY_DIRECTIVE>true</MY_DIRECTIVE>
<DefineConstants Condition="'$(MY_DIRECTIVE)' == 'true'">$(DefineConstants);MY_DIRECTIVE</DefineConstants>
</PropertyGroup>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.