首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 序列化与反序列化

C# 序列化与反序列化

作者头像
房上的猫
发布2018-03-14 12:47:46
9560
发布2018-03-14 12:47:46
举报
文章被收录于专栏:个人随笔个人随笔个人随笔

对象持久化到文本文件,策略是:将对象的属性值打散,拆解,分别存储。

序列化:  保存对象的"全景图"  序列化是将对象转换为可保存或可传输的格式的过程  三种:   二进制序列器:    对象序列化之后是二进制形式的,通过BinaryFormatter类来实现的,这个类位于System.Runtime.Serialization.Formatters.Binary命名空间下     [Serializable] //使对象可序列化(必须添加)      特性       程序集,类,方法,属性都可以使用特性       Java中注解 <==> C#特性     BinaryFormatter //创建二进制序列化器      Serialize(Stream(流),object(序列化对象))          流:可以理解成打通内存和硬盘的一个工具           输入流:从硬盘到内存           输出流:从内存到硬盘   XML序列化器:    对象序列化之后的结果符合SOAP协议,也就是可以通过SOAP?协议传输,通过System.Runtime.Serialization.Formatters.Soap命名空间下的SoapFormatter类来实现的。   SOAP序列化器:    对象序列化之后的结果是XML形式的,通过XmlSerializer?类来实现的,这个类位于System.Xml.Serialization命名空间下。XML序列化不能序列化私有数据。 反序列化:  将流转换为对象  Disk(硬盘)--->Cache(内存)  BinaryFormatter //创建二进制序列化器   Deserialize(Stream(流))//返回object类型 项目实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serializable_Deserialize
{
    /// <summary>
    /// 用户类
    /// </summary>
    
    //声明特性
    [Serializable]
    public class UserInfo
    {
        public UserInfo()
        {
        }

        public UserInfo(string userName, string userAddress,string path)
        {
            UserName = userName;
            UserAddress = userAddress;
            Path = path;
        }

        public string UserName { get; set; }
        public string UserAddress { get; set; }
        public string Path { get; set; }
    }
}

序列化:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Serializable_Deserialize
{
    /// <summary>
    /// 序列化
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            #region 序列化
            //集合初始化器    初始化数据
            List<UserInfo> list = new List<UserInfo>()
            {
                new UserInfo("房上的猫","北京海淀","https://www.cnblogs.com/lsy131479/"),
                new UserInfo("倾城月光~淡如水","北京大兴","http://www.cnblogs.com/fl72/")
            };
            Console.WriteLine("二进制序列化中...");
            //创建文件流派
            Stream stream = new FileStream("save.bin", FileMode.Create);
            //二进制序列化
            BinaryFormatter bf = new BinaryFormatter();
            //将对象或具有指定顶级 (根)、 对象图序列化到给定的流
            bf.Serialize(stream, list);
            //关闭流
            stream.Close();
            Console.WriteLine("二进制序列化成功!");
            #endregion

           
            Console.ReadLine();
        }
    }
}

反序列化:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Serializable_Deserialize
{
    /// <summary>
    /// 反序列化
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
           

            #region 反序列化
            //创建文件流派
            Stream stream = new FileStream("save.bin", FileMode.Open);
            //二进制序列化
            BinaryFormatter bf = new BinaryFormatter();
            //指定的流反序列化对象图
            List<UserInfo> list = (List<UserInfo>)bf.Deserialize(stream);
            //遍历反序列化后的泛型集合
            foreach (UserInfo item in list)
            {
                Console.WriteLine(item.UserName + ":" + item.Path);
            }
            //关闭流
            stream.Close();
            #endregion
            Console.ReadLine();
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档