尝试序列化对象列表失败

时间:2016-08-20 21:17:55

标签: .net vb.net serialization

我正在尝试将对象列表保存到文件中,但在此过程中抛出异常。这是我的对象类:

<Serializable()>
Public Class FavoritesObject
    Private Dset As DataSet
    Private Name As String
    Private BSource1 As BindingSource
    Private Bsource2 As BindingSource

    Public Sub New()
        ' Leave fields empty. 
    End Sub

    Public Sub New(ByVal datset As DataSet, ByVal thename As String, ByVal binsource1 As BindingSource, ByVal binsource2 As BindingSource)
        Dset = datset
        Name = thename
        BSource1 = binsource1
        binsource2 = binsource2
    End Sub

    Public Property Dataset1 As DataSet
        Get
            Return Dset
        End Get
        Set(ByVal value As DataSet)
            Dset = value
        End Set
    End Property

    Public Property FavoriteName As String
        Get
            Return Name
        End Get
        Set(ByVal value As String)
            Name = value
        End Set
    End Property

    Public Property BindingSource1 As BindingSource
        Get
           Return BSource1
        End Get
        Set(ByVal value As BindingSource)
            BSource1 = value
        End Set
    End Property

    Public Property BindingSource2 As BindingSource
        Get
            Return Bsource2
        End Get
        Set(ByVal value As BindingSource)
            Bsource2 = value
        End Set
    End Property
End Class

以下是序列化和反序列化的函数:

Public Sub WriteToBinaryFile(serializationFile As String, List As List(Of FavoritesObject))
    Using stream As Stream = File.Open(serializationFile, FileMode.Create)
        Dim bformatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        bformatter.Serialize(stream, List)
    End Using
End Sub

Public Function ReadFromBinaryFile(serializationFile As String)
    Using stream As Stream = File.Open(serializationFile, FileMode.Open)
        Dim bformatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        Dim favorites As List(Of FavoritesObject) = DirectCast(bformatter.Deserialize(stream), List(Of FavoritesObject))
        Return favorites
    End Using
End Function

当我尝试序列化对象列表时,我会抛出以下异常:

  

mscorlib.dll中发生未处理的“System.Runtime.Serialization.SerializationException”类型异常   附加信息:在Assembly'System.Windows.Forms中键入'System.Windows.Forms.BindingSource',Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'未标记为可序列化。

我对序列化很不熟悉,我正在努力学习它。有人可以说明我的问题可能是什么以及可能的解决办法吗?

2 个答案:

答案 0 :(得分:1)

在阅读了更多内容之后,我找到了这个解决方案:Preventing serialization of properties in VB.NET

这是似乎停止绑定源序列化的代码:

<NonSerialized()>
Private BSource1 As BindingSource
<NonSerialized()>
Private Bsource2 As BindingSource

答案 1 :(得分:0)

您拥有BindingSource类型的属性,该属性不可序列化, 您必须在不可序列化的属性上添加XmlIgnore属性,例如BindingSource2BindingSource1

代码将是

<Serializable()>
Public Class FavoritesObject
Private Dset As DataSet
Private Name As String
Private BSource1 As BindingSource
Private Bsource2 As BindingSource

Public Sub New()
    ' Leave fields empty. 
End Sub

Public Sub New(ByVal datset As DataSet, ByVal thename As String, ByVal binsource1 As BindingSource, ByVal binsource2 As BindingSource)
    Dset = datset
    Name = thename
    BSource1 = binsource1
    binsource2 = binsource2
End Sub

Public Property Dataset1 As DataSet
    Get
        Return Dset
    End Get
    Set(ByVal value As DataSet)
        Dset = value
    End Set
End Property

Public Property FavoriteName As String
    Get
        Return Name
    End Get
    Set(ByVal value As String)
        Name = value
    End Set
End Property

<NonSerialized()>
Public Property BindingSource1 As BindingSource
    Get
        Return BSource1
    End Get
    Set(ByVal value As BindingSource)
        BSource1 = value
    End Set
End Property

<NonSerialized()>
Public Property BindingSource2 As BindingSource
    Get
        Return Bsource2
    End Get
    Set(ByVal value As BindingSource)
        Bsource2 = value
    End Set
End Property
End Class