I have a large program (Net Framework 4.7, multiple solutions, each with multiple projects), and would like to be able to define a conditional compilation constant centrally and have it picked up. I've got a partial solution using a central properties file included by each csproj - this works fine in msbuild but not in Visual Studio 2019. This makes it tricky to develop this code, since Visual Studio shows the code requiring the constant as greyed out, even when it shouldn't be.
Central File (Test.Props):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TestProperty>YES</TestProperty>
</PropertyGroup>
</Project>
Main Project:
<Import Project="Test.props" />
...
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(TestProperty)' == 'YES' ">
<DefineConstants>$(DefineConstants);TEST</DefineConstants>
</PropertyGroup>
Test Code:
internal class Program
{
private static void Main(string[] args)
{
#if TEST
Console.WriteLine("Test Found");
#else
Console.WriteLine("Test Not Found");
#endif
Console.ReadLine();
}
}
Shows in Visual Studio as:
Any suggestions on how to accomplish this in a way which will work in Visual Studio 2019 and from msbuild directly? I would not expect any working solution to support the project properties, but I would like it to show the active code in the code editor!
update 1
. – Mr Qian