msbuildmsbuild-4.0

MSBuild 4.0 property functions cannot access properties inside of them


Is it a limitation of MSBuild 4.0 property functions that I cannot access a property from inside of one?

Here is an example that works just fine:

<PropertyGroup>
  <PartialConnection>$(TargetConnectionString.Substring( 0 + 12))</PartialConnection>
</PropertyGroup>

Here is another example that doe snot work. (I replace the 0 with another property)

<PropertyGroup>
  <LocationOfDataSource>$(TargetConnectionString.IndexOf("Data Source="))</LocationOfDataSource>
</PropertyGroup>
<Message Importance="high" Text="Location is = $(LocationOfDataSource)"/>
<PropertyGroup>
  <PartialConnection>$(TargetConnectionString.Substring( $(LocationOfDataSource) + 12))</PartialConnection>
</PropertyGroup>

this outputs

Location is = 0
Error MSB4184: The expression ""Data Source=MySQLServer;Integrated Security=True;Pooling=False".Substring(0 + 12)" cannot be evaluated. Input string was not in a correct format.

I took the output and plugged into a console app and it works just fine. I have tried several variations and I they always fail when I put a property inside a property function. (I even tried access the same property twice in a my property function and that failed too.)

Do property functions not support accessing properties?


Solution

  • I think my issue was assuming that math came for free.

    I needed to do this kind of thing:

    <PropertyGroup>
      <LocationOfDataSource>$(TargetConnectionString.IndexOf("Data Source="))</LocationOfDataSource>
      <LenthOfDataSourceString>12</LenthOfDataSourceString>
      <LocationOfEndOfDataSourceString>$([MSBuild]::Add($(LocationOfDataSource), $(LenthOfDataSourceString)))</LocationOfEndOfDataSourceString>
      <PartialConnectionString>$(TargetConnectionString.Substring($(LocationOfEndOfDataSourceString)))</PartialConnectionString>
    </PropertyGroup>
    

    Note that I am adding using Add($(Property), $(Property)) in this version. Add is one of the built-in MSBuild Property Functions (since MSBuild 4).

    It seems to be working now.