【问题标题】:Trying to serialize a list of objects fails尝试序列化对象列表失败
【发布时间】:2016-08-20 21:17:55
【问题描述】:

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

<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”类型的未处理异常 附加信息:在程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中键入“System.Windows.Forms.BindingSource”未标记为可序列化。

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

【问题讨论】:

  • BindingSource 不应该(也不能)被序列化的异常消息不是很明显
  • 下面的答案给出了直接的技术解决方案。更大的问题是您的设计,域实体不应包含 UI 或持久性代码。
  • @HenkHolterman 没意识到,谢谢。事后看来,根本没有理由像这样存储它们,因为它们可以即时创建。

标签: .net vb.net serialization


【解决方案1】:

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

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

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

【讨论】:

  • 已编辑。不小心把那个加进去了。
  • 这里的真正答案是从类中完全删除绑定源,因为它们不需要以这种方式存储。
【解决方案2】:

你有 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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多