When attempting to pass in an array of a specific enum type as a function argument via params object[], I am unable to pass in an actual array without casting to object[] first. But if I do so I do not have access to the original enum type.
Given the enum and function shown below, this works fine:
var works = SomeFunction(Season.Fall, Season.Winter);
However, this fails because it cannot implicitly convert to object, so it passes array as first argument as opposed to individual arguments:
var seasons = new Season[] {Season.Fall, Season.Winter};
var fail = SomeFunction(seasons);
It also cannot cast to object[] because then later it is unable to determine the original type. How can I use a predetermined array to pass it in?
[Flags]
public enum Season : byte
{
None = 0,
Spring = 1,
Summer = 2,
Fall = 4,
Winter = 8
}
...
public Something SomeFunction(params object[] values)
{
...
foreach (var value in values)
{
var type = value.GetType();
...
}
}
UPDATE: it works to convert the Season[] array to object[], but that seems hokey:
var seasons = new object[original.Length];
for (int i = 0; i < original.Length; i++)
{
seasons[i] = original[i];
}
params Season[] values?