Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know what you're doing wrong.</p> <p>Here is a <a href="http://linqpad.net" rel="nofollow">LINQPad</a> program that demonstrates, and it seems it uses the same calls that you do, yet I do get an output of the right type of array.</p> <pre><code>void Main() { object valueObj = new List&lt;object&gt; { 1, 2, 3 }; Type type = typeof(int[]); Type elementType = type.GetElementType(); MethodInfo castMethod = typeof(Enumerable).GetMethod("Cast") .MakeGenericMethod( new System.Type[]{ elementType } ); MethodInfo toArrayMethod = typeof(Enumerable).GetMethod("ToArray") .MakeGenericMethod( new System.Type[]{ elementType } ); var castedObjectEnum = castMethod.Invoke(null, new object[] { valueObj }); var castedObject = toArrayMethod.Invoke(null, new object[] { castedObjectEnum }); castedObject.Dump(); } </code></pre> <p>I <em>do</em> get an output corresponding to an array of int's. The code as posted seems to work just fine. Your problem must be elsewhere. Could it be that you're not actually sending in a property <code>List&lt;object&gt;</code> or that the list contains something that isn't an int?</p> <p>Just changing this line:</p> <pre><code>object valueObj = new List&lt;object&gt; { 1, 2, 3 }; </code></pre> <p>to this:</p> <pre><code>object valueObj = new List&lt;object&gt; { 1, 2, 3, "Test" }; </code></pre> <p>Gives me that exception.</p> <p>Perhaps you want <code>OfType</code> instead of <code>Cast</code>?</p> <p>If you replace the string in the <code>castMethod</code> retrieval, it again "works", but of course it will filter away that string.</p> <p>More information is needed here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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