C# 序列化和反序列化

来源:互联网 发布:买火车票下载什么软件 编辑:程序博客网 时间:2024/04/29 10:08
对stu类进行序列化和反序列化操作序列化所用到的stu类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication11{    [Serializable]    class student    {        public int stuid { get; set; }        public string stuname { get; set; }        public int stuage { get; set; }    }}
序列化和反序列化的主体操作
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace ConsoleApplication11{    [Serializable]    class Program    {        student s = new student();        static void Main(string[] args)        {            #region stu数组 序列化和反序列化            student[] st = new student[1];            st[0] = new student();            st[0].stuid = 23;            st[0].stuage = 23;            st[0].stuname = "猪";            FileStream fs = new FileStream(@"D:\log.txt", FileMode.Open, FileAccess.Write);//实例化一个FileStream流,FileStream有三个参数            BinaryFormatter bf = new BinaryFormatter();//实例化一个序列化对象            bf.Serialize(fs, st);            StreamWriter sw = new StreamWriter(fs);//将st数组以流的形式序列化            fs.Flush();//流的推送            fs.Close();//流的关闭            //反序列化            FileStream fs = new FileStream(@"d:\log.txt", FileMode.Open, FileAccess.Read);            BinaryFormatter bf = new BinaryFormatter();            student[] s = (student[])bf.Deserialize(fs);            foreach (student si in s)            {                Console.WriteLine("{0},{1},{2}", si.stuid, si.stuname, si.stuage);            }            fs.Flush();            fs.Close();            #endregion                  }    }}
下面是一个对图片的序列化和反序列化
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace WindowsFormsApplication9{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        ///         /// 图片序列化        ///         ///         ///         private void button1_Click(object sender, EventArgs e)        {            try            {                FileStream fs = new FileStream(@"d:\1.txt", FileMode.Open, FileAccess.Write);                StreamWriter sw = new StreamWriter(fs);                Bitmap bt = new Bitmap(@"D:\新建文件夹\qqshow2_boy.gif");                BinaryFormatter bf = new BinaryFormatter();                bf.Serialize(fs, bt);                fs.Flush();                fs.Close();            }            catch(Exception ex)            {                MessageBox.Show(ex.Message);            }        }        ///         /// 图片反序列化        ///         ///         ///         private void button2_Click(object sender, EventArgs e)        {            FileStream fs = new FileStream(@"d:\1.txt", FileMode.Open, FileAccess.Read);            BinaryFormatter bf = new BinaryFormatter();            pictureBox1.Image=(Image)bf.Deserialize(fs);            fs.Flush();            fs.Close();        }    }}
原创粉丝点击