以下示例創建一個3層樓,每層4個房間的簡單建築。這不是一個過於複雜的對象圖,但我認爲它應該可以實現這個想法。這個想法是,你可以實現System.Runtime.Serialization.ISerializable來控制序列化過程。
在這個例子中,我只對Floors類型實現了System.Runtime.Serialization.ISerializable。而在「樓層」類型中,我控制「房間」列表的序列化和反序列化。
不知道更多關於你的類型的具體細節,特別是重要計算的屬性,我不能專門與他們說話;不過,我認爲通過使用實現System.Runtime.Serialization.ISerializable提供的技術並控制序列化過程,您將能夠實現您正在查找的結果。請記住,您可以向序列化流中添加任何內容,並且不需要在類中添加所有內容;但是,在反序列化構造函數運行後,您至少需要讓您的類「正確」。您放入序列化流中的內容取決於您。
注意,但是,這個工程的二進制序列化,而不用於XML序列化。
Module Module1
Sub Main()
Dim floor1 As New Floor()
floor1.Rooms.Add(New Rooms(101))
floor1.Rooms.Add(New Rooms(102))
floor1.Rooms.Add(New Rooms(103))
floor1.Rooms.Add(New Rooms(104))
Dim floor2 As New Floor()
floor2.Rooms.Add(New Rooms(201))
floor2.Rooms.Add(New Rooms(202))
floor2.Rooms.Add(New Rooms(203))
floor2.Rooms.Add(New Rooms(204))
Dim floor3 As New Floor()
floor3.Rooms.Add(New Rooms(301))
floor3.Rooms.Add(New Rooms(302))
floor3.Rooms.Add(New Rooms(303))
floor3.Rooms.Add(New Rooms(304))
Dim tower As New Building()
tower.Floors.Add(floor1)
tower.Floors.Add(floor2)
tower.Floors.Add(floor3)
Dim byteArray As Byte() = Serialized(tower)
Dim backAgain As Building = DeSerialized(byteArray)
End Sub
Private Function Serialized(ByVal tower As Building) As Byte()
Using ms As New System.IO.MemoryStream()
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
formatter.Serialize(ms, tower)
Return ms.GetBuffer()
End Using
End Function
Private Function DeSerialized(ByVal byteArray As Byte()) As Building
Using ms As New System.IO.MemoryStream(byteArray)
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
ms.Position = 0
Return DirectCast(formatter.Deserialize(ms), Building)
End Using
End Function
End Module
<Serializable()>
Public Class Building
Public Sub New()
End Sub
Private _Floors As New List(Of Floor)
Public ReadOnly Property Floors As List(Of Floor)
Get
Return _Floors
End Get
End Property
End Class
<Serializable()>
Public Class Floor
Implements System.Runtime.Serialization.ISerializable
Public Sub New()
End Sub
Public Sub New(ByVal info As Runtime.Serialization.SerializationInfo, ByVal context As Runtime.Serialization.StreamingContext)
Dim counter As Integer = info.GetInt32("RoomCount")
For index As Integer = 0 To counter - 1
_Rooms.Add(DirectCast(info.GetValue(String.Format("Room{0}", index.ToString()), GetType(Rooms)), Rooms))
Next
End Sub
Private _Rooms As New List(Of Rooms)
Public ReadOnly Property Rooms As List(Of Rooms)
Get
Return _Rooms
End Get
End Property
Public Sub GetObjectData(info As System.Runtime.Serialization.SerializationInfo, context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
info.AddValue("RoomCount", _Rooms.Count)
For index As Integer = 0 To _Rooms.Count - 1
info.AddValue(String.Format("Room{0}", index.ToString()), _Rooms(index))
Next
End Sub
End Class
<Serializable()>
Public Class Rooms
Public Sub New()
End Sub
Public Sub New(ByVal roomNum As Integer)
Me.New()
_RoomNumber = roomNum
End Sub
Private _RoomNumber As Integer = 0
Public Readonly Property RoomNumber As Integer
Get
Return _RoomNumber
End Get
End Property
End Class
嗨蘭迪,謝謝你的迴應!我知道提供GetObjectData的ISerializable接口和功能。但在我的情況下,我的對象用[DataContract]標記,在這種情況下我不能使用ISerializable接口。現在我正在查看IDataContractSurrogate接口以及它提供的內容。無論如何,再次感謝您的迴應! – 2012-07-06 19:24:52