Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>I think your code would be <em>much</em> simpler if you made this a generic method:</p> <pre><code>public static List&lt;object&gt; ConvertToListOfObjects&lt;T&gt;(List&lt;T&gt; list) { return list.ConvertAll&lt;object&gt;(t =&gt; t); } </code></pre> <p>Then you just need to find and invoke <em>that</em> method generically:</p> <pre><code>MethodInfo method = typeof(Foo).GetMethod("ConvertToListOfObjects", BindingFlags.Static | BindingFlags.Public); Type listType = list.GetType().GetGenericArguments()[0]; MethodInfo concrete = method.MakeGenericMethod(new [] { listType }); List&lt;object&gt; objectList = (List&lt;object&gt;) concrete.Invoke(null, new object[]{list}); </code></pre> <p>Complete example:</p> <pre><code>using System; using System.Reflection; using System.Collections.Generic; class Test { public static List&lt;object&gt; ConvertToListOfObjects&lt;T&gt;(List&lt;T&gt; list) { return list.ConvertAll&lt;object&gt;(t =&gt; t); } static void Main() { object list = new List&lt;int&gt; { 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&lt;object&gt; objectList = (List&lt;object&gt;) concrete.Invoke(null, new object[] {list}); foreach (object o in objectList) { Console.WriteLine(o); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload