title: Convert boxed ienumerable into object array without knowing explicit type
body: <p>I've got a little code that pulls a boxed ienumerable instance out of an expression tree. The code needs to convert it into object[].</p>
  
  <p>So far I've gotten away with assuming it'll be <code>IEnumerable&lt;string&gt;</code> and thus casting it followed with <code>.ToArray()</code>.</p>
  
  <p>Circumstances have changed and now it could also possibly be a <code>IEnumerable&lt;int&gt;</code>. I still need to change the box into an <code>object[]</code> and don't really care what the type is.</p>
  
  <hr>
  
  <pre><code>object list = Expression.Lambda(methodCallExpression.Object).Compile().DynamicInvoke();
  
  var enumerable = (IEnumerable&lt;string&gt;)list;
  object[] values = enumerable.ToArray();
  </code></pre>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>Strings are references and Integers are value types. I've found that while I can box the reference to an array of ints, I cannot pretend it's an array of boxed ints as int's cannot be boxed.</p>
  
  <blockquote>
    <p>An exception of type 'System.InvalidCastException' occurred in
    System.Core.dll but was not handled in user code  Additional
    information: Unable to cast object of type
    'System.Collections.Generic.List<code>1[System.Int32]' to type
    'System.Collections.Generic.IEnumerable</code>1[System.Object]'.</p>
  </blockquote>
  
  <pre><code>object list = Expression.Lambda(methodCallExpression.Object).Compile().DynamicInvoke(); 
  var enumerable = (IEnumerable&lt;object&gt;)list; 
  object[] values = enumerable.ToArray();
  </code></pre>