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: