. Net methods for automatic serialization and deserialization of objects are implemented through reflection. Therefore, we can not only serialize public fields, but also serialize private fields and basic fields.
. Net stores the object state in stream in serialization. Therefore, you can serialize an object to any media by selecting an appropriate stream type. For example, files, networks, and XML. However, static fields cannot be serialized.
Serializable attributes
You can use the serializable attribute in the class definition to indicate that the class is serializable.
[Serializable]
Public class myclass
{
Private int ID;
Private string name;
}
Nonserialized attributes
In a serializable class, what if a member cannot be serialized? The solution is to mark the attribute of the class member as nonserialized.
1 [serializable]
2 public class myclass
3 {
4 private int ID;
5
6 private string name;
7
8 [nonserialized]
9 private object state;
10}
Ideserializationcallback Interface
During deserialization,. Net initializes each non-serializable member variable to the default value of this type. Then you can set the variable to the correct value in the provided method. To achieve this, we need to complete the defined processing method after deserialization. In this case, the serialization class must implement the ideserializationcallback interface. After the. NET deserialization object, the ideserializationcallback method ondeserialization () is called to complete our custom deserialization steps.