我正在學習類型提供程序,它看起來像一個突破性功能。但是,我無法設法使用JsonProvider對json進行反序列化,以便目標類型具有Generic.Dictionary屬性。它可以用Json.NET完成。下面是代碼:可以JsonProvider反序列化到Generic.Dictionary?
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
默認。這可以重寫嗎?