0

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.

Caio Sant'Anna
  • 304
  • 4
  • 22
  • I don't know exactly but try to use dynamic keyword instead of var – Chestera Mar 11 '20 at 21:40
  • 1
    The important question to ask yourself is "what do you want to do with the array, after you have converted the type?" Do you want to set `PropertyName` to that array? – Sweeper Mar 11 '20 at 21:43
  • 1
    There are a lot of important details left out of your question. However, taken literally it seems that the issue is creating an instance of an array when you only know the type at runtime. See marked duplicate. – Peter Duniho Mar 11 '20 at 21:50
  • @Sweeper, I added more information related to your comment. – Caio Sant'Anna Mar 12 '20 at 00:49
  • @PeterDuniho your suggestion creates an empty array based on an dynamic type and on a lenght to up to 3 dimensions, correct ? I need to create the converted array based on the elements of a string.split. Let's suppose I have a string like "1;2;3". The split will result on a string array with 3 elements. The conversion should take those 3 elements, the dynamic type, in this case, Int32, and convert it to an Int32[] that will then be stored dynamically in an objects property that matches the dynamic conversion.Is it possible to achieve that with what you recommended ? – Caio Sant'Anna Mar 12 '20 at 00:59
  • Yes, it's possible. You would initialize each array element explicitly, but otherwise it should be the same. Alternatively, if you really want to use `ConvertAll()`, then you should look at [this question and its answers](https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method) – Peter Duniho Mar 12 '20 at 01:37

0 Answers0