msbuildvcxproj

How to check string contains a string in msbuild?


I have a problem writing a targets file for my vcxproj project. In my targets file the first thing I want to set some build properties based on the configuration so I have this:

  <Choose>
    <When Condition="$(Configuration.Contains('Debug', StringComparison.OrdinalIgnoreCase))">
      <PropertyGroup>
      </PropertyGroup>
    </When>
  </Choose>

But when I try to open the project with imported target file I get an error

error  : The expression ""Debug".Contains(Debug, StringComparison.OrdinalIgnoreCase)" cannot be evaluated. Method 'System.String.Contains' not found.

I am using Visual Studio 2022 Preview. Acording to MSBuild docs the string is just a .Net5 string and should have the method. Could someone help me please?


Solution

  • That overload of Contains is only available in recent .Net versions, not in e.g. 4.8 so that is why it is not found. Workaround: convert to lower then compare

    <Choose>
      <When Condition="$(Configuration.ToLowerInvariant().Contains('debug'))">
      </When>
    </Choose>