public static List ConvertToListOfObjects(List list)
{
return list.ConvertAll(t => t);
}
然后你只需要找到并调用该方法:
MethodInfo method = typeof(Foo).GetMethod("ConvertToListOfObjects",
BindingFlags.Static | BindingFlags.Public);
Type listType = list.GetType().GetGenericArguments()[0];
MethodInfo concrete = method.MakeGenericMethod(new [] { listType });
List objectList = (List) concrete.Invoke(null,
new object[]{list});
完整的例子:
using System;
using System.Reflection;
using System.Collections.Generic;
class Test
{
public static List ConvertToListOfObjects(List list)
{
return list.ConvertAll(t => t);
}
static void Main()
{
object list = new List { 1, 2, 3, 4 };
MethodInfo method = typeof(Test).GetMethod("ConvertToListOfObjects",
BindingFlags.Static | BindingFlags.Public);
Type listType = list.GetType().GetGenericArguments()[0];
MethodInfo concrete = method.MakeGenericMethod(new [] { listType });
List objectList = (List) concrete.Invoke(null,
new object[] {list});
foreach (object o in objectList)
{
Console.WriteLine(o);
}
}
}