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 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:
Then you just need to find and invoke that method generically:
Complete example:
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: