0

I am trying to save a list of objects to file but getting an exception thrown in the process. This is my object class:

<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

Here are my functions for serialization and deserialization:

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

When I try to serialize my list of objects I get the following exception thrown:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional information: Type 'System.Windows.Forms.BindingSource' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

I am really unfamiliar with serialization and am trying to learn it. Can someone please shed some light on what my issue may be and a possible fix?

Blackwood
  • 4,504
  • 16
  • 32
  • 41
Zach Johnson
  • 2,047
  • 6
  • 24
  • 40
  • Isn't that obvious from the exception message that `BindingSource` is not supposed to (and can't) be serialized – Ivan Stoev Aug 20 '16 at 21:29
  • 2
    The answer below gives the direct technical solution. The bigger problem is your design, a domain entity should not contain UI or Persistence code. – H H Aug 20 '16 at 21:32
  • @HenkHolterman Didn't realize that thanks. In hindsight there was no reason to store them like that at all as they can be created on the fly. – Zach Johnson Aug 21 '16 at 18:27

2 Answers2

1

After reading a bit more I found this solution: Preventing serialization of properties in VB.NET

This is the code that seems to stop the serialization of the binding sources:

<NonSerialized()>
Private BSource1 As BindingSource
<NonSerialized()>
Private Bsource2 As BindingSource
Community
  • 1
  • 1
Zach Johnson
  • 2,047
  • 6
  • 24
  • 40
0

You have properties of type BindingSource which is not serializable, you have to add the XmlIgnore attribute on the properties which are not serializable, like BindingSource2 and BindingSource1

The code will be

<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
Tarek Abo ELkheir
  • 1,311
  • 10
  • 13