我在vb.net中有一個web服務,它返回json格式的數據。其中一個數據項可以採用許多不同類型的值:Boolean
,String
,Dictionary(of String, String)
或Dictionary(Of String, Object)
後者允許返回靈活的數據列表。然後在響應中分別指定一個itemType和DataType,以便三方知道期望什麼。這工作得很好。vb.net無法將Dictionary(Of String,List(Of String))轉換爲Object
不過,我現在收到以下錯誤試圖返回一個Dictionary(Of String, Dictionary(Of String, List(Of String)))
:
Value of type 'System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.List(Of String)))' cannot be converted to 'System.Collections.Generic.Dictionary(Of String, Object)'.
這是很碰巧採取Dictionary(Of String, Dictionary(Of String, String))
但不是Dictionary(Of String, Dictionary(Of String, List(Of String)))
。我很困惑 - 我雖然幾乎可以轉換成Object
?爲什麼Dictionary(Of String, String)
可以轉換爲對象而不是Dictionary(Of String, List(Of String))
?
我可以繞着它通過執行以下操作:
Dim Bar As New Dictionary(Of String, Dictionary(Of String, List(Of String)))
' Add stuff to bar here
Dim Foo As New Dictionary(Of String, Object)
For Each Row As KeyValuePair(Of String, Dictionary(Of String, List(Of String))) In Bar
Foo.Add(Row.Key, New Dictionary(Of String, Object))
For Each Item As KeyValuePair(Of String, List(Of String)) In Row.Value
Foo(Row.Key).add(Item.Key, Item.Value)
Next
Next
,但我不明白爲什麼我需要。有什麼我失蹤,可能會導致問題後,任何人都可以解釋什麼樣的對象不能投到Object
?