C#序列化/反序列化对象

北大青鸟大学城校区logo 北大青鸟大学城校区
招生简章校园环境师资力量就业明星招生问答软件工程师北京大学学历学员项目联系我们 报名通道

免费在线咨询通道>>

免费在线报名通道>>

北大青鸟报名电话
当前位置:北大青鸟 > 软件教程 > .net教程 >

C#序列化/反序列化对象

标签:   分类:.net教程


   using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Runtime.Serialization.Formatters.Binary;

    using System.IO; using System.IO.Compression;

    using System.Windows.Forms;

    namespace Vjsdn_Tester

    {

    public class TestZipObject

    {

    /// <summary>

    ///测试简单序列化/反序列化对象

    /// </summary>

    public void Test()

    {

    SerilizabledObject obj = new SerilizabledObject();

    byte[] bs = ZipObject.CompressionObject(obj); object o = ZipObject.DecompressionObject(bs); //还原,ok MessageBox.Show((o as SerilizabledObject).getName());

    }

    /// <summary>

    /// 序列化对象,将对象写入文件,然后还原.

    /// </summary>

    public void TestReadWriteFile()

    {

    SerilizabledObject obj = new SerilizabledObject();

    byte[] bs = ZipObject.CompressionObject(obj);

    //将数组写入文件

    Stream writer = new FileStream("E:\\test.shape", FileMode.Create, FileAccess.Write, FileShare.Write); writer.Write(bs, 0, bs.Length); writer.Flush(); writer.Close();

    //读取文件,先写入数组,再从数组转换为对象.Easy! FileStream fs = File.Open("E:\\test.shape", FileMode.Open); byte[] bss = new byte[fs.Length]; int i = fs.Read(bss, 0, (int)fs.Length);

    object o = ZipObject.DecompressionObject(bss); //还原,ok MessageBox.Show((o as SerilizabledObject).getName());

    }

    }

    /// <summary>

    ///测试用对象

    /// </summary>

    [Serializable]

    public class SerilizabledObject

    {

    string id = "id001";

    string name = "www.vjsdn.com";

    public SerilizabledObject() { }

    public string getName()

    {

    return this.name;

    }

    }

    /// <summary>

    ///压缩解压object

    /// </summary>

    public class ZipObject

    {

    #region压缩解压object

    public static byte[] CompressionObject(object DataOriginal)

    {

    if (DataOriginal == null) return null;

    BinaryFormatter bFormatter = new BinaryFormatter();

    MemoryStream mStream = new MemoryStream();

    bFormatter.Serialize(mStream, DataOriginal); byte[] bytes = mStream.ToArray();

    MemoryStream oStream = new MemoryStream();

    DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress); zipStream.Write(bytes, 0, bytes.Length); zipStream.Flush(); zipStream.Close(); return oStream.ToArray();

    }

    public static object DecompressionObject(byte[] bytes)

    {

    if (bytes == null) return null;

    MemoryStream mStream = new MemoryStream(bytes);

    mStream.Seek(0, SeekOrigin.Begin); DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);

    object dsResult = null;

    BinaryFormatter bFormatter = new BinaryFormatter();

    dsResult = (object)bFormatter.Deserialize(unZipStream);

    return dsResult;

    }

    #endregion

    }

    }

    测试代码:

    private void button7_Click(object sender, EventArgs e)

    {

    new TestZipObject().Test(); // 测试简单序列化/反序列化对象new TestZipObject().TestReadWriteFile(); // 序列化对象,将对象写入文件,然后还原.

    }


若有疑问请拨打北大青鸟咨询热线:010-80146691或点击免费在线咨询!
  • xml地图 网站地图 招生简章 合作企业 学员项目 联系我们
  • 关闭窗口