我正在学习类型提供程序,它看起来是一项开创性的功能。但是,我无法使用JsonProvider反序列化具有Generic.Dictionary属性的目标类型的json。可以使用Json.NET完成。以下是代码:
type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}""">
type ByHand(d:Dictionary<string,int>) =
member val dict = d with get,set
let text = """{"dict":{"C":3,"D":4}}"""
let newt = JsonConvert.DeserializeObject<ByHand> text
let prov = ByProv.Parse text
printfn "%A" newt.dict.["C"]
//Can only call statically inferred A or B (and it will throw at run-time)
printfn "%A" prov.Dict.A
显然,默认情况下,dict
被推断为记录类型而不是 Dictionary
。这个可以被覆盖吗?