|
-
Sep 6th, 2011, 11:01 AM
#10
Thread Starter
Member
Re: Binary Serialization and Custom Collections
Okay, I've finally figured out how to deserialize my custom collection. I don't know if this is how it's supposed to be done, but it works. Here is the code:
Code:
Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
' retrieve the count from the file
Dim iCnt As Integer = info.GetInt32("FGSDataCount")
' get each of the items from the file based on the count
For i As Integer = 0 To iCnt - 1
Me.Add(info.GetValue("FGSData" & i.ToString, GetType(FGSData)))
Next
End Sub
Public Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) Implements ISerializable.GetObjectData
' store the count to the file
info.AddValue("FGSDataCount", Me.Count)
' iterate through each item and add it to the file
For i As Integer = 0 To Me.Count - 1
info.AddValue("FGSData" & i.ToString, Me.Item(i))
Next
End Sub
So, first I save the collection count to the file. Then, I iterate through the list of items in the collection, and add each one to the file, giving each a unique name (based on position in list). Then when I want to deserialize the file, I first retrieve the count from the file. Then, I loop from 0 to count and using the unique name, I add each item back to my collection.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|