| Start |
|---|
| Serialization Binder The binder gives you an opportunity to inspect what types are being loaded in your application domain Create a class inherited from SerializationBinder Now we can check what types are loading and on this basis to decide what we really want to receive For using a binder, you must add it to the BinaryFormatter. The complete solution |
| Controlling serialization behavior with attributes If you use the Assume we want to add a "Rating" to our Vector and we want to make sure the value always starts at 1. The way it is written below, it will be 0 after being deserialized: To fix this problem, we can simply add the following method inside of the class to set it to 1: Or, if we want to set it to a calculated value, we can wait for it to be finished deserializing and then set it: Similarly, we can control how things are written out by using |
| Some gotchas in backward compatibility This small example shows how you can lose backward compatibility in your programs if you do not take care in advance about this. And ways to get more control of serialization process At first, we will write an example of the first version of the program: Version 1 And now, let us assume that in the second version of the program added a new class. And we need to store it in an array. Now code will look like this: Version 2 And code for serialize and deserialize And so, what would happen when you serialize the data in the program of v2 and will try to deserialize them in the program of v1? You get an exception: Why? The ObjectManager has a different logic to resolve dependencies for arrays and for reference and value types. We added an array of new the reference type which is absent in our assembly. When ObjectManager attempts to resolve dependencies it builds the graph. When it sees the array, it can not fix it immediately, so that it creates a dummy reference and then fixes the array later. And since this type is not in the assembly and dependencies can’t be fixed. For some reason, it does not remove the array from the list of elements for the fixes and at the end, it throws an exception “IncorrectNumberOfFixups”. It is some ‘gotchas’ in the process of serialization. For some reason, it does not work correctly only for arrays of new reference types. And the first way to fix it and maintain compatibility? |
| Making an object serializable Add the All members will be serialized unless we explicitly opt-out using the All members are required to be present on deserialization unless marked with |
| Serialization surrogates (Implementing ISerializationSurrogate) Implements a serialization surrogate selector that allows one object to perform serialization and deserialization of another As well allows to properly serialize or deserialize a class that is not itself serializable Implement ISerializationSurrogate interface Then you need to let your IFormatter know about the surrogates by defining and initializing a SurrogateSelector and assigning it to your IFormatter Even if the class is not marked serializable. The complete solution |
| Adding more control by implementing ISerializable That would get more control over serialization, how to save and load types Implement ISerializable interface and create an empty constructor to compile For data serialization, you can specify the desired name and the desired type When the data is deserialized, you will be able to read the desired type RemarksThe binary serialization engine is part of the .NET framework, but the examples given here are specific to C#. As compared to other serialization engines built into the .NET framework, the binary serializer is fast and efficient and usually requires very little extra code to get it to work. However, it is also less tolerant to code changes; that is, if you serialize an object and then make a slight change to the object's definition, it likely will not deserialize correctly. |
Read Article
https://develop.horje.com/learn/1434/reference