Serialization byte array vs XML file

前端 未结 7 1861
礼貌的吻别
礼貌的吻别 2021-01-06 08:42

I am heavily using byte array to transfer objects, primitive data, over the network and back. I adapt java\'s approach, by having a type implement ISerializable, which cont

相关标签:
7条回答
  • 2021-01-06 09:19

    This should be fine, but you're doing work that is already done for you. Look at the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter class.

    Rather than needing to implement your own Read/WriteOjbectData() methods for each specific type you can just use this class that can already handle most any object. It basically takes an exact copy of the memory representation of almost any .Net object and writes it to or reads it from a stream:

    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(outputStream, objectToSerialize);
    
    objectToDeserialize = bf.Deserialize(inputStream) as DeserializedType;
    

    Make sure you read through the linked documents: there can be issues with unicode strings, and an exact memory representation isn't always appropriate (things like open Sockets, for example).

    0 讨论(0)
提交回复
热议问题