2
votes

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?

4
how about List<object> you can use itEhsan Sajjad

4 Answers

4
votes

Make your method generic:

 private List<T> GetAsList<T>(string value)
 {
    List<T> list = new List<T>();
    var items = value.Split('|');
    foreach (var item in items)
    {
       list.Add((T)Convert.ChangeType(item, typeof(T)));
    }
    return list;
 }

And use it like this:

List<int> listOfInt = GetAsList<int>("1|2|3");
List<long> listOfLong = GetAsList<long>("5|6|7");
List<double> listOfDouble = GetAsList<double>("1.1|2|3.3");

If you don't know the type at compile time, you can call GetAsList using reflection:

var propTypeDouble = typeof(List<double>);
var listOfDoubleRuntime = typeof([DelaringType])
                         .GetMethod("GetAsList")
                         .MakeGenericMethod(propTypeDouble.GetGenericArguments().First())
                         .Invoke(null, new[] {"1.1|2.2|3.3"});
0
votes

If possible, I would make your method generic, as recommended by others, however, if that is not possible, you can create an instance using the Type parameter like this:

Type targetType = typeof(List<>).MakeGenericType(propertyType);
IList result = Activator.CreateInstance(targetType);
0
votes

You should use something like this -

     private static List<T> GetAsList<T>(string value) 
        {
            List<T> list = new List<T>();
            var items = value.Split('|');
            foreach (var item in items)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
                if (converter != null)
                {
                    list.Add((T)converter.ConvertFrom(item));
                }
            }
            return list;
        }

And consume like -

        List<Int32> demoList = new List<Int32>();

        demoList = GetAsList<Int32>("1|2|5|7");
0
votes

List< T> list;

is anonymous type until you want to create an instance of it.

List< dynamic> list; is always a dynamic type even after you created an instance of it

List< dynamic> list = new List< dynamic>()