I’m currently having the following method which requires params – I cannot change this methods definition:
public static DoStuff(params Parameter[] p){ /* ... */ }
I do not know how many parameters I’ll have at runtime, so I want to pass an array or a list – I’ve thought about using Iterators like this:
List<Parameter> pList = new List<Parameter>();
pList.Add(new Parameter("@a", "aaaa!");
pList.Add(new Parameter("@b", "bbbb!");
pList.Add(new Parameter("@c", "cccc!");
DoStuff(Iterate(pList));
And here’s the Iterate-Method:
public static IEnumerator<Parameter> Iterate(List<Parameter> pList)
{
foreach (Parameter p in pList)
yield return p;
}
Unfortunately, it does not work, it keeps telling me that it can’t cast (generated type) to CommandParameter.
Any help (or different approaches) would be appreciated!
Edit: It appears, in an attempt to simplify my solution for the sake of making my question understandable I trivialized my problem.
In addition to this array, I’d like to also pass “normal” Parameters like this:
DoStuff(Iterate(pList), new Parameter("@d", "dddd!"));
I do this merely out of curiosity to see whether it works – but this does not work simply by casting my List to an Array and appending the next Parameter.
Thanks,
Dennis
You could do it like that: