C#序列化反序列化

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
namespace 序列化
{   
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.age = "1";
            p.name = "n";
            //序列化
            //using (FileStream file = new FileStream(@"C:\Users\DELL\Desktop\a.txt", FileMode.OpenOrCreate, FileAccess.Write))
            //{
            //    BinaryFormatter bf = new BinaryFormatter();
            //    bf.Serialize(file, p);
            //}
            //反序列化
            using(FileStream file=new FileStream(@"C:\Users\DELL\Desktop\a.txt",FileMode.OpenOrCreate,FileAccess.Read))
            {
                Person t;
                BinaryFormatter bf = new BinaryFormatter();
                t = (Person)bf.Deserialize(file);
                Console.WriteLine(t.age);
                Console.Read();
            }
                    }
    [Serializable]//對類標識
    class Person
    {
        public string name;
        public string age;
    }
}
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章