I have a method that reads some XML and populates an object. This is done via reflection based on the element names in the XML mathing the property names on the object. This works for basic object types but i am struggling with enum arrays.
So I have a PropertyInfo object (lets call it property) for my enum array property and I have a string value (lets call it value) containing comma separated numbers representing the enum values (e.g. "1,3,5").
I have tried:
property.SetValue(this, value.Split(',').Select(i => int.Parse(i)).ToArray(), null);
and
property.SetValue(this, value.Split(',').Select(i => Enum.ToObject(property.PropertyType.GetElementType(), int.Parse(i))).ToArray(), null);
But no joy. In the first code example the result of the Select.ToArray is an int[] which then throws a parse error. Similar case with the second but the Select.ToArray returns an object[] and again throws a parse error.
I want to write this as if the enum type is unknown.
Any ideas?