Search code examples
msbuildcsproj

DefineConstants in addition to existing ones in csproj


It seems like if I have a csproj file like the following, I end up with BAR defined and FOO not defined.

<PropertyGroup>
  <DefineConstants>FOO</DefineConstants>
</PropertyGroup>
<PropertyGroup>
  <DefineConstants>BAR</DefineConstants>
</PropertyGroup>

Is there a syntax for "Define additional constants" so that I can use it and end up with both FOO and BAR defined?

I am aware that in this contrived example, I could just have

<PropertyGroup>
  <DefineConstants>FOO BAR</DefineConstants>
</PropertyGroup>

But my actual use case is more complicated. I really need to be able to define a constant in addition to whatever was set before.


Solution

  • This does it:

    <PropertyGroup>
      <DefineConstants>FOO</DefineConstants>
    </PropertyGroup>
    <PropertyGroup>
      <DefineConstants>$(DefineConstants);BAR</DefineConstants>
    </PropertyGroup>