0

I have a custom task written in C#. This custom task has an argument defined like this:

public required string[] StringArray { get; set; }

Then in a .csproj file I have these definitions:

<ItemGroup>
    <StringElemements Include="A" />
    <StringElemements Include="B" />
    <StringElemements Include="C" />
</ItemGroup>

<Target Name="MyTarget">
    <MyCustomTask StringArray="@(StringElements)" />
</Target>

Now when the task executes, I expected to have the values 'A', 'B', and 'C' in the property StringArray and that the task is being run once. However, when building the project, the task is being called for every element in the item group and then just pass a single element to the property. So in this example, the task is being executed three times while passing the values 'A', 'B', and 'C' separately to each task invocation.

I have been trying with transformations to get it right. If I call the task like this:

<Target Name="MyTarget">
    <MyCustomTask StringArray="A;B;C" />
</Target>

then all three elements are passed to the StringArray property of my custom task, which is what I want. Is there any way to achieve this? I need to have the StringElements in an item group like they are now, since they are needed in that form elsewhere in the build process.

3
  • Shouldn't it be StringList instead of StringArray? Commented 4 hours ago
  • The name of the property of the custom task is StringArray (see first code snippet). Changing the type (string[]) to List<string> or Collection<string> is not supported by msbuild. Commented 2 hours ago
  • An ItemGroup is passed to a task as an ITaskItem[], not a string[]. Commented 1 hour ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.