0
JavaScriptSerializer
을 사용하여 List(Of Int32)
을 직렬화하려고합니다.List (Int32)의 List가 JSON을 ArrayList로 serialize하는 이유는 무엇입니까?
Unable to cast object of type 'System.Collections.ArrayList' to type 'System.Collections.Generic.List`1[System.Int32]'.
내 코드는 다음과 같습니다 : 나는 어떤 점에서 나는이 오류가 다시 목록에 Deserialize
에 개체를 시도 할 때까지 작동하는 것 같다. 누구든지 왜 올바른 유형으로 명시 적으로 캐스트해도 목록이 ArrayList로 자동 저장되는 이유를 설명 할 수 있습니까?
미리 감사드립니다.
Private Property _serialized As String
Get
Return ViewState("_serialized")
End Get
Set(value As String)
ViewState("_serialized") = value
End Set
End Property
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim l As New List(Of Int32) From {2, 4, 6, 8, 10}
Dim d As New Dictionary(Of String, Object)
d.Add("myList", l)
Dim js As New Script.Serialization.JavaScriptSerializer
Me._serialized = js.Serialize(d)
End If
End Sub
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
Dim js As New Script.Serialization.JavaScriptSerializer
Dim d As Dictionary(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(Me._serialized)
Dim l As List(Of Int32) = CType(d.Item("myList"), List(Of Int32))
For Each i As Int32 In l
Trace.Warn(i.ToString)
Next
End Sub
에러 라인이있다 :
Dim l As List(Of Int32) = CType(d.Item("myList"), List(Of Int32))
환상적입니다. 저는 거의 10 년 동안 .NET을 사용 해왔고'.Cast (Of Integer) '에 대해 전혀 몰랐습니다. 매력과 같은 작품 - 대단히 감사합니다! – EvilDr