標籤:des style blog http color io ar for sp
1.序列化 還原序列化
C#中如果需要:將一個結構很複雜的類的Object Storage Service起來,或者通過網路傳輸到遠端用戶端程式中去,這時就需要用到序列化,還原序列化(Serialization & Deserialization)
2.BinaryFormattter
.NET中串列有三種,BinaryFormatter, SoapFormatter和XmlSerializer.
其中BinaryFormattter最簡單,它是直接用二進位方式把對象 (Object)進行串列或反串,他的優點是速度快,可以串列private或者protected的member, 在不同版本的。NET中都相容,可以看作是。NET自己的本命方法,當然缺點也就隨之而來了,離開了。NET它就活不了,所以不能在其他平台或跨網路上進 行。
3.序列化
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, DS);
byte[] buffer = ms.ToArray();
MemoryStream :建立其支援儲存區為記憶體的流
4.還原序列化
//還原序列化:將byte[]型的資料,放到Stream中,BinaryFormatter將流中的資料還原序列化成對象
MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter ser = new BinaryFormatter();
DataSetSurrogate dss = ser.Deserialize(ms) asDataSetSurrogate;
5.小結
進行序列化,還原序列化,利用到的都是BinaryFormate,都得借普通流MemoryStream,不同的是:
序列化時,將對象序列化後放到MemoryStream,而還原序列化時,將MemoryStream中的byte[]資料,反序列成對象
- SerializeUtil.rar (571 Bytes)
.net 序列化與還原序列化