title: How cast a generic value:'T to an array<'R> on F#
body: <p>I'm building reflection for a <a href="https://github.com/xyncro/chiron" rel="nofollow noreferrer">JSON parser</a>, and hit a road when serializing arrays:</p>
  
  <pre><code>let rec toJson (value:'T) =
      let t = typedefof&lt;'T&gt;
  
      match t with
      | _ when FSharpType.IsRecord(t) -&gt;
          let fields = fieldsOf&lt;'T&gt;
          let values = seq {
              for field in fields do
                  let x = field.GetValue value
  
                  let result = toField field x
                  yield field.Name.ToLower(), result
          }               
          values |&gt; Seq.filter(fun (_, x) -&gt; x &lt;&gt; Null) |&gt; List.ofSeq 
          |&gt; Chiron.JsonObject.ofPropertyList 
          |&gt; Chiron.JsonObject.toJson
          |&gt; Chiron.Formatting.Json.format
      | _ when t.IsArray -&gt;
          let te = t.GetElementType()
  
          let values = value :? seq&lt;obj&gt; &lt;-- How turn value into a array/seq?
          let txt = value :?&gt; IEnumerable |&gt; Seq.map toJson |&gt; String.concat ""
          txt
      | _ -&gt;
          _toField t value
          |&gt; Chiron.Formatting.Json.format
  
  let rows:Log.LogSyncRow array = query...
  
  rows |&gt; toJson
  </code></pre>
  
  <p>The problem is <code>(value:'T)</code> is a record or a array of records, and can't see how enumerate them. I try with <a href="https://stackoverflow.com/questions/2140079/how-to-cast-an-object-to-a-list-of-generic-type-in-f">How to cast an object to a list of generic type in F#</a> but not work:</p>
  
  <pre><code>let values = value :?&gt; seq&lt;obj&gt; 
  
  Error FS0008: This runtime coercion or type test from type    'T     to     seq&lt;obj&gt;    involves an indeterminate type based on information prior to this program point. Runtime type tests are not allowed on some types. Further type annotations are needed. (FS0008) (Server)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>value :?&gt; IEnumerable
  
  Error FS0033: The type 'System.Collections.Generic.IEnumerable&lt;_&gt;' expects 1 type argument(s) but is given 0 (FS0033) (Server)
  </code></pre>