vicsz
vicsz

Reputation: 9764

Referencing an MSBuild property using the contents of another property

I want to be able to reference an MSBuild (3) property using the contents of another property. For example:

<PropertyGroup>
    <SelectVariable>Test</SelectVariable>
    <TestVariable>1</TestVariable>
    <FullVariable>2</FullVariable>
</PropertyGroup>

<Message Text="Value $($(SelectVariable)Variable)"/>

In this scenario, I want the contents of TestVariable outputted (1). Is this possible?

Upvotes: 3

Views: 698

Answers (3)

Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi

Reputation: 44342

Sure this is possible. Just do:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <SelectVariable>Test</SelectVariable>
    <TestVariable>1</TestVariable>
    <FullVariable>2</FullVariable>
  </PropertyGroup>

  <Target Name="Demo01">
    <PropertyGroup>
      <Value>$(SelectVariable)Variable</Value>
    </PropertyGroup>
    <Message Text="Value $(Value)"/>
  </Target>

</Project>

The result is shown in the image below.

Alt text

Upvotes: 3

Ruben Bartelink
Ruben Bartelink

Reputation: 61893

You could use the <Choose> task to achieve something similar, but (as Peter said) that's likely to be some distance from your desire to have something short and pithy.

Perhaps psake is the answer - it has no such arbitrary and puny limits when nesting expressions and parentheses :P

Upvotes: 1

Peter McEvoy
Peter McEvoy

Reputation: 2926

I don't believe that is possible. However, you could achieve a similar effect with ItemGroups:

<PropertyGroup>
    <SelectVariable>Test</SelectVariable>
</PropertyGroup>

<ItemGroup>
    <Variable Include="1">
        <Select>Test</Select>
    </Variable>
    <Variable Include="2">
        <Select>Full</Select>
    </Variable>
</ItemGroup>

<Message Text="@(Variable)"
         Condition=" '%(Select)' == '$(SelectVariable)' " />

It's a little clunky tho...

Upvotes: 2

Related Questions