1

我正在寻找一种方法来序列化包含不同类型对象(如字符串、整数和集合)的结构。

我有这个数据结构:

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(List(Of booking)) 创建单独的文件?

谢谢!

4

1 回答 1

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属性应用于属性:

<XmlArray("bookings")> _
Public Property bookingList As IList(Of booking)
    ...same code as above...
End Property
于 2017-06-20T10:27:28.387 回答