What is the point of the ISerializable interface?

后端 未结 4 1518
礼貌的吻别
礼貌的吻别 2020-11-29 00:06

It seems like I can serialize classes that don\'t have that interface, so I am unclear on its purpose.

4条回答
  •  生来不讨喜
    2020-11-29 01:02

    In order to make an object "transportable", you have to serialize it. For example, if you want to transfer object data using .NET Remoting or Web Services you have to provide methods which serializes your object data, reducing your object instances into a transportable format that represents a high-fidelity representation of the object.

    You then may also take the serialized representation, transport it to another context such as a different machine, and rebuild your original object.

    When implementing the ISerializable interface, a class must provide the GetObjectData method that is included in the interface, as well as a specialized constructor that is specialized to accept two parameters: an instance of SerializationInfo, and an instance of StreamingContext.

    If your classes don't require fine-grained control of their object state, then you could just use [Serializable] attribute. Classes that require more control over the serialization process can implement the ISerializable interface.

提交回复
热议问题