What is the point of the ISerializable interface?

后端 未结 4 1510
礼貌的吻别
礼貌的吻别 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 00:49

    With the ISerializable you can write custom methods in your object to take over serialization when doing binary serialization, to serialize your objects in a different manner than what the default approach used by BinaryFormatter will do.

    In other words, if the default approach serializes your object in a different manner than how you want it to serialize as, you can implement ISerializable for complete control. Note that hand in hand with ISerializable, there's also a custom constructor you should implement.

    XmlSerialization will of course only use properties, ISerializable has nothing to do with XML serialization.

    Thanks Marc and Pop for the comments, I was a bit hasty with my first answer.

提交回复
热议问题