4

I would guess it has to be an ITaskItem since it's a vector instead of scalar, I've got the only 2 MsBuild books here on my desk, and I can't find examples of how to pass in an array to a task. I want to do a string array, but I'd like to know the proper way that would work with any primitive type.

How do you pass in an array of string (or int) to a MsBuild task?

Maslow
  • 18,464
  • 20
  • 106
  • 193

2 Answers2

10

MSBuild tasks can accept ITaskItem, primitives, string or an array of any of those for parameters. You just declare the type in your task and then the values will be converted before passed to the task. If the value cannot convert to the type then an exception will be raised and the build will be stopped.

For example if you have a task which accepts an int[] named Values then you could do.

<Target Name="MyTarget">
    <MyTask Values="1;45;657" />
    <!-- or you can do -->
    <ItemGroup>
        <SomeValues Include="7;54;568;432;79" />
    </ItemGroup>

   <MyTask Values="@(SomeValues) />
</Target>

Both approaches are essentially the same. The other answers stating that all parameters are strings or that you have to use ITaskItem are incorrect.

You said you have two books on MSBuild, then I presume one is my Inside the Microsoft Build Engine book, you should read the chapter on Custom Tasks so that you get a full grasp on these topics. There is a section explaining parameter types specifically.

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
  • +1 thanks for clearing that up. btw, your ItemGroup tag isn't closed, I imagine it was supposed to close after the SomeValues tag, yes? – Maslow Oct 14 '10 at 12:47
  • I can't seem to get String[] to work in the same fashion. One could use a less elegant String.Split(delimiter) solution, but a clear API is preferred. – Martin R-L Jan 13 '11 at 08:54
  • 2
    Found it! You need to use ';' as your delimeter, not ','. – Martin R-L Jan 13 '11 at 09:20
  • 1
    the sample is showing the incorrect use of comma as a delimiter, please update the sample to show the proper use of semicolons. otherwise this sample is great. – Shaun Wilson May 04 '11 at 19:35
  • Your link http://sedotech.com/Resources/InsideMSBuildBook is broken – Prof. Falken Feb 10 '20 at 15:00
0

IIRC, msbuild items are always string arrays - that is the only option. So an array of integers would be stored as an array numeric strings.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234
  • so then it'd be an include list of values ``? Msbuild would automatically convert to `int[]` if the custom task asked for `int[]` and was passed `ITaskItem[]` ? – Maslow Oct 13 '10 at 20:18