The function WriteStartAttribute can be called with 1, 2 or 3 strings as arguments. The amount I want to call it with is dependent on the amount of arguments in writeInfo. To clarify, I want to do the following using a loop:
if (writeInfo.Count == 2)
{
writer.WriteStartAttribute(writeInfo[1]);
}
else if (writeInfo.Count == 3)
{
writer.WriteStartAttribute(writeInfo[1], writeInfo[2]);
}
else if (writeInfo.Count == 4)
{
writer.WriteStartAttribute(writeInfo[1], writeInfo[2], writeInfo[4]);
}
I tried it using an array (and a List) like this:
for (int i = 0; writeInfo.Count() - 1 < i; i += 1)
{
argumentList[i] = writeInfo[i + 1];
}
writer.WriteStartAttribute(argumentList);
However, because no overload accepts an array (or a List) this does not work.
How can I call a function with differing amount of arguments using a loop?
Assuming this is the XmlTextWriter.WriteStartAttribute, it doesn’t accept
paramsso you’ll have to manually handle it.Create your own
paramsmethod, and then put yourifstatements in there:Then call that in your loop:
Bonus points for making it an extension method: