Controlling serialization behavior with attributes

suggest change

If you use the [NonSerialized] attribute, then that member will always have its default value after deserialization (ex. 0 for an int, null for string, false for a bool, etc.), regardless of any initialization done in the object itself (constructors, declarations, etc.). To compensate, the attributes [OnDeserializing] (called just BEFORE deserializing) and [OnDeserialized] (called just AFTER deserializing) together with their counterparts, [OnSerializing] and [OnSerialized] are provided.

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:

[Serializable]
public class Vector
{
    public int X;
    public int Y;
    public int Z;

    [NonSerialized]
    public decimal Rating = 1M;

    public Vector()
    {
        Rating = 1M;
    }

    public Vector(decimal initialRating)
    {
        Rating = initialRating;
    }
}

To fix this problem, we can simply add the following method inside of the class to set it to 1:

[OnDeserializing]
void OnDeserializing(StreamingContext context)
{
    Rating = 1M;
}

Or, if we want to set it to a calculated value, we can wait for it to be finished deserializing and then set it:

[OnDeserialized]
void OnDeserialized(StreamingContext context)
{
    Rating = 1 + ((X+Y+Z)/3);
}

Similarly, we can control how things are written out by using [OnSerializing] and [OnSerialized].

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Binary Serialization:
* Controlling serialization behavior with attributes

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
122 Binary Serialization
127 Caching
135 Pointers
147 C# Script