我正在寻找一种方法来序列化包含不同类型对象的结构,如字符串,整数和集合。
我有这个数据结构:
Dim database as New mainStruct
<Serializable()> Structure mainStruct
Public name As String
Public address As String
Public Shared bookings As IList(Of booking) = New List(Of booking)
End Structure
<Serializable()> Structure booking
Public id As Integer
Public category As String
Public description As String
End Structure
运行序列化程序:
Dim fs As FileStream = New FileStream("x.bin", FileMode.OpenOrCreate)
Dim serial As New XmlSerializer(GetType(mainStruct))
serial.Serialize(fs, database)
fs.Close()
输出仅包含所有非集合变量,例如:
<?xml version="1.0"?>
<mainStruct xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>foo</name>
<address>bar</address>
</mainStruct>
我如何序列化预订集合,而无需使用GetType创建单独的文件(列表(预订))?
谢谢!
答案 0 :(得分:0)
共享成员未序列化,因为序列化完全是为了保存类/结构 实例 ,而共享成员不是实例的一部分。
在你的结构中添加一个实例属性,你应该好好去:
Public Property bookingList As IList(Of booking)
Get
Return mainStruct.bookings
End Get
Set(value As IList(Of booking)
mainStruct.bookings = value
End Set
End Property
如果您希望将代码设为<bookings>
,则只需将XmlArray
attribute应用于该媒体资源:
<XmlArray("bookings")> _
Public Property bookingList As IList(Of booking)
...same code as above...
End Property