Skip to content

Instantly share code, notes, and snippets.

@wattsm
Last active December 20, 2015 04:19
Show Gist options
  • Save wattsm/6070195 to your computer and use it in GitHub Desktop.
Save wattsm/6070195 to your computer and use it in GitHub Desktop.
Creating an F# list via reflection
let makeListOf itemType (items : obj list) =
let listType =
makeGenericType
<| typedefof<Microsoft.FSharp.Collections.List<_>>
<| [ itemType; ]
let add =
let cons =
listType.GetMethod ("Cons")
fun item list ->
cons.Invoke (null, [| item; list; |])
let list =
let empty =
listType.GetProperty ("Empty")
empty.GetValue (null)
list
|> List.foldBack add items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment