Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild tasks can accept primitive arrays, but how do you write one to pass into the task?

Tags:

msbuild

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?

like image 510
Maslow Avatar asked Dec 29 '22 06:12

Maslow


1 Answers

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.

like image 174
Sayed Ibrahim Hashimi Avatar answered Jan 01 '23 16:01

Sayed Ibrahim Hashimi