C#對象的序列化與反序列化 2

    using System;     using System.IO;     using System.IO.Compression;     using System.Reflection;     using System.Runtime.Serialization.Formatters.Binary;     using System.Text;    public static string Serialize(object obj)         {             MemoryStream stream = new MemoryStream();             GZipStream serializationStream = new GZipStream(stream, CompressionMode.Compress);             new BinaryFormatter().Serialize(serializationStream, obj);             serializationStream.Dispose();             return Convert.ToBase64String(stream.ToArray());         }     public static object Deserialize(string base64Str)         {             BinaryFormatter formatter = new BinaryFormatter();             MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Str));             GZipStream serializationStream = new GZipStream(stream, CompressionMode.Decompress);             return formatter.Deserialize(serializationStream);         }
相關文章
相關標籤/搜索