Converting List<Object> using a System.Type

2019-08-31 16:35发布

问题:

Let's say I have retrieved a System.Type object using reflection and want to use that type to convert a List<Object> into another List of that type.

If I try:

Type type = GetTypeUsingReflection();
var myNewList = listObject.ConvertAll(x => Convert.ChangeType(x, type)); 

I get an exception since the object does not implement the IConvertible interface. Is there a way around this or another way to approach this problem?

回答1:

Type type = typeof(int); // could as well be obtained by Reflection

var objList = new List<object> { 1, 2, 3 };
var intList = (IList) Activator.CreateInstance(
    typeof(List<>).MakeGenericType(type)
    );

foreach (var item in objList)
    intList.Add(item);

// System.Collections.Generic.List`1[[System.Int32, ...]]
Console.WriteLine(intList.GetType().FullName);

But why on Earth would you need it?



回答2:

Your proposed solution wouldn't actually work anyway - it'll just create another List<Object>, because the return type of ChangeType is Object.

Assuming you just want casting, you could do something like this:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

class Test
{
    private static List<T> ConvertListImpl<T>(List<object> list)
    {
        return list.ConvertAll(x => (T) x);
    }

    // Replace "Test" with the name of the type containing this method
    private static MethodInfo methodDefinition = typeof(Test).GetMethod
        ("ConvertListImpl", BindingFlags.Static | BindingFlags.NonPublic);

    public static IEnumerable ConvertList(List<object> list, Type type)
    {
        MethodInfo method = methodDefinition.MakeGenericMethod(type);
        return (IEnumerable) method.Invoke(null, new object[] { list });
    }

    static void Main()
    {
        List<object> objects = new List<object> { "Hello", "there" };
        List<string> strings = (List<string>) ConvertList(objects,
                                                          typeof(string));

        foreach (string x in strings)
        {
            Console.WriteLine(x);
        }
    }
}


回答3:

Casting is of little use when the type is not known at design-time. Once you have your new list of objects cast to your new type, how are you going to make use of the new type? You can't call a method that the type exposes (without using more reflection)



回答4:

There's no way for the type system to go from a type variable storing type T to a generic parameter of type T.

Technically you can create a generic list of the correct type (using reflection), but the type information is not available at compile time.