views:

1284

answers:

2

I want to do this:

MethodInfo m = myList.GetType().GetMethod("ConvertAll", System.Reflection.BindingFlags.InvokeMethod).MakeGenericMethod(typeof(object));
List<object> myConvertedList = (List<object>)m.Invoke(myList, new object[]{ (t => (object)t)});

myList is a generic list of a specific type (unknown to the application), and I want to convert it to a list of objects to do some operations.

However this fails with this error: "Cannot convert lambda expression to type 'object' because it is not a delegate type"

Can you help me find what's wrong? Am I trying to do something that's not possible?

Is there some other way to achieve the same thing?

A: 

A lambda forms a method group (basically this is a method identified by name (and scope) only. Since methods with the same name can be overloaded, a method group comprises several different members). This cannot always implicitly be converted to a delegate because a delegate is actually bound to a single method from within a method group. This plays a role with overloading.

Unfortunately, the same applies in your case. The remedy is to make an explicit delegate:

List<object> myConvertedList = (List<object>)m.Invoke(myList, new object[]{ new Func<YourType, object>(t => (object)t)});
Konrad Rudolph
The difficulty is that YourType isn't known at compile-time here - hence my suggestion to turn it into a generic method.
Jon Skeet
Ah damn, I didn't pay attention to that.
Konrad Rudolph
Yes, I was about to comment the same thing... Thanks anyway
Juan Manuel
+4  A: 

A lambda expression is convertible to either a delegate type or an expression tree with the right signature - but you need to specify which delegate type it is.

I think your code would be much simpler if you made this a generic method:

public static List<object> ConvertToListOfObjects<T>(List<T> list)
{
    return list.ConvertAll<object>(t => t);
}

Then you just need to find and invoke that method generically:

MethodInfo method = typeof(Foo).GetMethod("ConvertToListOfObjects",
    BindingFlags.Static | BindingFlags.Public);
Type listType = list.GetType().GetGenericArguments()[0];
MethodInfo concrete = method.MakeGenericMethod(new [] { listType });
List<object> objectList = (List<object>) concrete.Invoke(null, 
                                                   new object[]{list});

Complete example:

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

class Test
{
    public static List<object> ConvertToListOfObjects<T>(List<T> list)
    {
        return list.ConvertAll<object>(t => t);
    }

    static void Main()
    {
        object list = new List<int> { 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<object> objectList = (List<object>) concrete.Invoke(null,
                                                    new object[] {list});

        foreach (object o in objectList)
        {
            Console.WriteLine(o);
        }
    }
}
Jon Skeet