彩票走势图

什么是C#序列化/反序列化

原创|其它|编辑:郝浩|2009-09-29 14:13:41.000|阅读 994 次

概述:本文介绍了什么是C#序列化/反序列化。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

    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 = "";
    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(); // 序列化对象,将对象写入文件,然后还原.
    }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:IT专家网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP