views:

523

answers:

1

I'm trying out MVVM in VB.Net for a while now and started out with some of my entities using List(of T)s, which I xml serialized to disk. Now, I updated the classes used by the Lists to implement INotifyPropertyChanged so I also changed the List(of T)s to ObservableCollection(of T)s.

After which, the XML serializer stopped working :'( A colleague told me that ObservableCollections, unlike generic Lists, are not serializable.

If so then how can I make them Serializable? Thanks in Advance~! :D

+1  A: 

Your college is half correct. ObservableCollection(Of T) is indeed serializable but it is so through the binary serializer, not the XML one.

What you can do to work around this to wrap the serialization of any collections of ObservableCollection(Of T) with List(Of T). Just do the conversion at the point of serialization.

For example ...

Public Sub Serialize(ByVal col as ObservableCollection(Of Integer))
  Dim list = New List(Of Integer)(col)
  ReallySerialize(list)
End Sub

Public Function Unserialize() As ObserableCollection(Of Integer)
  Dim list = ReallyUnserialize()
  return New ObservableCollection(Of Integer)(list)
End Function
JaredPar
I should have thought of that~! T_T; Thanks~!
GaiusSensei