c#中序列化和反序列化的理解

 

 

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

 

序列化:对象转化为文件的过程(字节流)

反序列化:文件(字节流)转化为对象的过程

 

        private string _Path = @"D:\LearningSerilizable.txt";

        /// <summary>
        /// 进行序列化
        /// </summary>
        public void Test1()
        {
            Person p = new Person("children", 19);

            if (File.Exists(_Path))
            {
                using (FileStream fs = new FileStream(_Path, FileMode.OpenOrCreate))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, p);
                }
            }   
        }

 

对一个对象序列化过程:

1:对象应该是可以被序列化[Serializable]

2:序列化过程还是需要文件流,定义文件流,确定路径。

3:使用BinaryFormatter进行序列化和反序列化。

 

 

 

还有梦的时候就别轻易选择放弃,追求梦想的过程你真的很美!

 

posted @ 2019-02-28 19:46  彩色的梦  阅读(483)  评论(0编辑  收藏  举报