I have a input string, which is received by a file. It contains some values, separated by ; E.g. 1;2;3
I want to convert this to a List. The property is retrived by reflection, so i don't have any fix list type.
In short: As inputs i have a PropertyInfo (PropertyType) and a string value. I want to get a list of type "PropertyType" as output.
I would like to do something like
private List<object> GetAsList(Type propertyType, string value)
{
List<object> list = new List<object>();
var items = value.Split('|');
foreach (var item in items)
{
list.Add(Convert.ChangeType(item, propertyType.GetGenericArguments().Single()));
}
return list;
}
The problem here is, that I can't assign a List to, lets say a List(). I could use something like this:
if(propertyType == typeof(List<Int32>)
list = new List<Int32>();
else if(propertyType == typeof(List<Int64>)
list = new List<Int64>();
[...]
Which isn't a nice solution in my opinion. Do you have any idea how to solve this problem?
List<object>
you can use it – Ehsan Sajjad