How do I get a discriminated union case from a string?
问题 I have a discriminated union and I want to select a case based on a string (which is read from a JSON file). This is easy to do: type MyDU = A | B let str = "B" let myDU : MyDU = match str with | "A" -> MyDU.A | "B" -> MyDU.B | _ -> failwith "whatever" // val myDU : MyDU = B However, sometimes there are many cases, which would require a lot of typing. The Microsoft.FSharp.Reflection library allows me to get a UnionCaseInfo object: open Microsoft.FSharp.Reflection let myDUInfo : UnionCaseInfo