Some time ago I asked a question about serializing a collection to a byte array and saving that. It resulting in this blog post. I modified the Serialize and Deserialize functions to take a generic T type and I created collections which extend from ObservableCollection<t>. I uploaded a sample here.
I also have an add function which looks like this:
Adding the first element is not a problem. When I add the second element I see that the Persisting event handler is called (I would have expected this to happen only when I'm actually calling .Save()) and it results in an exception:
What am I doing wrong here? In the project I also included some extension methods: ToCollection() and ToByteArray(). If I use those the serialization works, but when I try to retrieve an item from the database on the second run of the program it throws an exception saying _segmentCollection.Count throw a NullReferenceException. The collection is correctly initialized as far as I can tell. |
Hello jensen, After some investigation I've understood that the problem might be in the following:
|
Hello jensen,
I'll check the case. Thanks for the sample!
I currently worked around it by creating FillFromByteArray functions (instead of ToCollection) in the respective classes by using a ISerializationSurrogate implementation for each respective class that inherits from ObservableCollection.
I also tried by converting my collections to a standard List<t> before serializing but that eventually threw the same context exception.