Mariano
Mariano

Reputation: 1073

Using Conditional Symbol inside .csproj

Inside the .csproj there are some constants defined like this:

<DefineConstants>DEBUG;TRACE;ANDROID;GLES;OPENGL;OPENAL</DefineConstants>

Then later in the project there's an itemgroup

<ItemGroup>
<EmbeddedNativeLibrary Include="..\ThirdParty\Dependencies\openal-soft\libs\armeabi-v7a\libopenal32.so">
  <Platforms>Android,Ouya</Platforms>
  <Link>libs\armeabi-v7a\libopenal32.so</Link>
</EmbeddedNativeLibrary>
<EmbeddedNativeLibrary Include="..\ThirdParty\Dependencies\openal-soft\libs\armeabi\libopenal32.so">
  <Platforms>Android,Ouya</Platforms>
  <Link>libs\armeabi\libopenal32.so</Link>
</EmbeddedNativeLibrary>
<EmbeddedNativeLibrary Include="..\ThirdParty\Dependencies\openal-soft\libs\x86\libopenal32.so">
  <Platforms>Android,Ouya</Platforms>
  <Link>libs\x86\libopenal32.so</Link>
</EmbeddedNativeLibrary>

I want this ItemGroup to be included only when the constant OPENAL is defined, regardless of debug or release. How can I do this?

<ItemGroup Condition="XXXXXX" >

What would XXXXXX be?

Upvotes: 10

Views: 7029

Answers (3)

Rick Strahl
Rick Strahl

Reputation: 17681

I found that this also works:

<ItemGroup Condition="MY_CONSTANT == 'MY_CONSTANT'">

Apparently the value evaluates to a string. A little easier for me to remember than the $DefineConstants.Contains() syntax.

Here's an example of how I use it:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
    <DefineConstants>INCLUDE_AUTHORIZENET,INCLUDE_BRAINTREE</DefineConstants>
    <Version>1.3</Version>
        <TargetFrameworks>net472;NET6.0;net8.0</TargetFrameworks>
    </PropertyGroup>
    <ItemGroup>   
      <PackageReference Include="Westwind.Utilities" Version="5.0.1" />
    <PackageReference Include="Braintree" Version="5.25.0" Condition="INCLUDE_BRAINTREE == 'INCLUDE_BRAINTREE'" />    
    <Reference Include="AuthorizeNet" Condition="INCLUDE_AUTHORIZENET == 'INCLUDE_AUTHORIZENET'">
      <!--<PackageReference Include="AuthorizeNet" Version="2.0.3" />-->
      <!-- 
        This is a .NET FRAMEWORK reference but it works in Core, so we load manually to avoid project warnings for mimatched target    
      -->
      <HintPath>.\_SupportPackages\AuthorizeNet.dll</HintPath>
    </Reference>
    </ItemGroup>
</Project>

Upvotes: 0

Jorgen Thelin
Jorgen Thelin

Reputation: 1076

You can also use the <Choose> element to create conditional blocks in msbuild / .csproj files if you want to do more sophisticated conditional processing.

http://msdn.microsoft.com/en-us/library/ms164282.aspx

Edited: Angle brackets had disappeared.

Upvotes: 3

Mariano
Mariano

Reputation: 1073

The syntax for a condition that checks if a Constant is defined is: (in this case OPENAL)

<ItemGroup Condition="$(DefineConstants.Contains('OPENAL'))">

Upvotes: 17

Related Questions