'How to add custom semicolon-delimited list of property to msbuild taks properties attribute

Here is simple test project

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="configure">
    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
    <PropertyGroup>
        <param>var1=val1,var2=val2</param>
    </PropertyGroup>
    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
    <Target Name="configure" DependsOnTargets="" Outputs="">
        <MSBuild
            Projects="$(MSBuildProjectFullPath)"
            Properties="component=c1;$(param.Replace(',',';'))"
            Targets="process"/>
    </Target>
    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
    <Target Name="process">
        <Message Text="[$(component)][$(var1)][$(var2)]"/>
    </Target>
</Project>

i expect to see

[c1][val1][val2]

but unfortunately msbuild don't parse additional properties from my string and output

[c1][val1;var2=val2][]

Any ideas how to help msbuild? Thanks

Solution: Use [MSBuild]::Unescape

<PropertyGroup>
    <p>var1=val1,var2=val2</p>
    <param>$([MSBuild]::Unescape($(p.Replace(',',';'))))</param>
</PropertyGroup>


Solution 1:[1]

You should use an item group to logically group the collection of parameters and values and use an MSBUILD transform to get the output you wish to see.

<Target Name="SemiColon">
    <ItemGroup>
        <Parm Include="Parm1">
            <Value>Val1</Value>
        </Parm>
        <Parm Include="Parm2">
            <Value>Val2</Value>
        </Parm>
        <Parm Include="Parm3">
            <Value>Val3</Value>
        </Parm>
        <Parm Include="Parm4">
            <Value>Val4</Value>
        </Parm>
        <ParamLine Include="%(Parm.identity )=%(Parm.Value)" />
    </ItemGroup>
    <Message Importance="High" 
        Text="Parameter Name and Value: @(ParamLine, ';')" />
    <Message Importance="High"
        Text="Just values: @(Parm -> MetaData('Value'))" />
</Target>

The ParamLine item uses an MsBuild transform to format the output in Param=Val format, while the second just displays the values separated by semicolons.

Parameter Name and Value: Parm1=Val1;Parm2=Val2;Parm3=Val3;Parm4=Val4
Just values: Val1;Val2;Val3;Val4

Solution 2:[2]

Use MSBuild escape special characters

%3B for ; and %2C for ,.

Example from MS:

<Compile Include="MyFile.cs%3BMyClass.cs"/>

https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-escape-special-characters-in-msbuild?view=vs-2022#msbuild-special-characters

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicodemeus
Solution 2 Ogglas