Is it possible to get MSBuild property by string name?

Viewed 90

I am trying to retrieve an MSBuild property ($(Something)) by using the string value "Something" stored in another property. I tried to use MSBuild's Escape/Unescape methods but it still just prints $(Something) and not the property I want:

  <Target Name="OutputBuildMacro">
    <PropertyGroup>
      <PropertyToGet>MSBuildProjectFile</PropertyToGet>
      <MacroName>$([MSBuild]::Escape('$'))($(PropertyToGet))</MacroName>
    </PropertyGroup>
    <Message Text="$([MSBuild]::Unescape('$(MacroName)'))" />
  </Target>

Output when I run msbuild TestProject.vcxproj -t:OutputBuildMacro

Project "D:\TestProject\TestProject.vcxproj" on node 1 (OutputBuildMacro target(s)).
OutputBuildMacro:
  $(MSBuildProjectFile)
Done Building Project "D:\TestProject\TestProject.vcxproj" (OutputBuildMacro target(s
)).

I also tried to wrap the unescaped value with another pair of $( and ), but that just gives me an error. Is it possible to retrieve msbuild property using a string value rather than literal name written in XML?

1 Answers

You can have a try it:

  <Target Name="OutputBuildMacro">
    <PropertyGroup>
      <PropertyToGet>MSBuildProjectFile</PropertyToGet>
      <MacroName>$(PropertyToGet)</MacroName>
    </PropertyGroup>
    <Message Text="$(MacroName)" />
  </Target>

>Is it possible to retrieve msbuild property using a string value rather than literal name written in XML?

As far as I know, it is impossible. The literal name is unique, but the string value isn't.

Related