I'm trying to convert a string array to an array whose type is going to be known only at runtime. I tried that in two different ways :
PropertyInfo info = obj.GetType().GetProperty("PropertyName");
//GetElementType() because in this case the dynamic element is an array
var converter = TypeDescriptor.GetConverter(info.PropertyType.GetElementType());
var firstWay = stringProperty.Split(';').Select(x => converter.ConvertFrom(x)).ToArray();
var secondWay = Array.ConvertAll(stringProperty.Split(';'), x => Convert.ChangeType(x, info.PropertyType.GetElementType()));
Both, the first and second way return an array of objects. I needed the array to be of the type of the property info (info.PropertyType.GetElementType()). Let's suppose the info.PropertyType is Int32, decimal, bool. I wanted the converted array to match this type instead of returing a generic array of objects. How can I do that ?
Regarding @Sweeper comment... I intent to use the converted array to set another object property dynamically as well. Something like this:
obj2.GetType().GetProperty("PropertyName").SetValue(obj2, firstWay, null);
Basically, the first array will be the result of a split from a string (stringProperty.Split(';')). That array should then be converted to that dynamic type that will at the end be stored in an object property that has that dynamic type used on the array conversion.