Given a .Net type, say typeof<string>
, at runtime, how do I create the equivalent of string list = []
?
My motivation is that when using FSharpValue.MakeRecord
to create a record based on the parsed values, the values ββshould be passed as obj[]
. I finished the arguments with box
and it worked, except for the lists. The problem I am facing is that an empty list of untyped lists cannot be put in a box and then unpacked. The specific error returned:
System.InvalidCastException: Unable to cast object of type 'Microsoft.FSharp.Collections.FSharpList`1[System.Object]' to type 'Microsoft.FSharp.Collections.FSharpList`1[System.String]'.
An empty typed list can be put in a box and unpacked, so I tried to find a way to make a list for the runtime type, for example. a Type returned by typeof <> but with no luck.
type Test = {Names : string list} // fails let genericList = [] {Names = unbox (box genericList)} //works let typedList : string list = [] {Names = unbox (box typedList)} //works let genericNonEmptyList = ["Bill"] {Names = unbox (box genericNonEmptyList)}
source share