0
votes

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:

VS Screenshot

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!

1
Actually, it shows the normal behavior in my side. Did you try to create a new project to test it?Mr Qian
I have found the issue. Please see update 1.Mr Qian

1 Answers

1
votes

Actually, the behavior shows correctly in my side. And in VS IDE, t'he section under if TEST will be highlighted.

Note: you should use $(DefineConstants) rather than $DefineConstants and the former is the way to get the MSBuild property value.

This is what I did.

enter image description here

enter image description here

enter image description here

So due to your issue, I suggest you could try the following steps to troubleshoot the issue:

Suggestion

1) disable any third party extensions under Extensions menu-->Manage Extensions

2) close VS Instance, delete .vs hidden folder under the solution folder, bin and obj folder.

3) repair VS or update it to the latest version if there is a new version.

Then, restart your project to test again.

Besides, try to create a new project to test whether the issue happens in the new one. If the new one does not have it, you could try to migrate the content from the old one into the new one.

============================

Update 1

I have found the reason. When you change the value of TestProperty or do other modification, you should close VS Instance first, delete .vs hidden folder to remove the previous record and enable the new modification.