C#–序列化與還原序列化

來源:互聯網
上載者:User

引用:

序列化與還原序列化

解釋:

序列化,就是將一個對象儲存到檔案中去,而還原序列化就是將該檔案重新儲存為一個對象.

序列化的三種方式:

1.BinaryFormatter

    class BinarySerialize : ISerialize    {        FileStream fs;        string filepath = @"D:/1/1.txt";        public void Serialize(Book book)        {            using (fs = new FileStream(filepath, FileMode.Create))            {                BinaryFormatter formate = new BinaryFormatter();                formate.Serialize(fs, book);            }        }        public Book DeSerialize()        {            Book book;            using (fs = new FileStream(filepath, FileMode.Open))            {                BinaryFormatter formate = new BinaryFormatter();                book = (Book)formate.Deserialize(fs);            }            return book;        }    }
序列化之後結果如下:
 

2.SoapFormatter

    class SoapSerialize : ISerialize    {        FileStream fs;        string filepath = @"D:/1/2.txt";        public void Serialize(Book book)        {            using (fs = new FileStream(filepath, FileMode.Create))            {                SoapFormatter formate = new SoapFormatter();                formate.Serialize(fs, book);            }        }        public Book DeSerialize()        {            Book book;            using (fs = new FileStream(filepath, FileMode.Open))            {                SoapFormatter formate = new SoapFormatter();                book = (Book)formate.Deserialize(fs);            }            return book;        }    }
序列化之後,結果如下;
 

3.XmlSerializer

    class XMLSeria : ISerialize    {        FileStream fs;        string filepath = @"D:/1/3.txt";        public void Serialize(Book book)        {            using (fs = new FileStream(filepath, FileMode.Create))            {                XmlSerializer formate = new XmlSerializer(typeof(Book));                formate.Serialize(fs, book);            }        }        public Book DeSerialize()        {            Book book;            using (fs = new FileStream(filepath, FileMode.Open))            {                XmlSerializer formate = new XmlSerializer(typeof(Book));                book = (Book)formate.Deserialize(fs);            }            return book;        }    }
序列化之後,結果如下:
 
完整源碼如下:
程式碼片段1:
定義類庫Define
namespace Define{    [Serializable]    public class Book    {        string bookname;        public string BookName        {            get            {                return this.bookname;            }            set            {                this.bookname = value;            }        }        int price;        public int Price        {            get            {                return this.price;            }            set            {                this.price = value;            }        }        public override string ToString()        {            return "BookName:" + this.bookname + "<br/>Price:" + this.price.ToString() + "<br/>";        }    }    public interface ISerialize    {        void Serialize(Book book);        Book DeSerialize();    }    class BinarySerialize : ISerialize    {        FileStream fs;        string filepath = @"D:/1/1.txt";        public void Serialize(Book book)        {            using (fs = new FileStream(filepath, FileMode.Create))            {                BinaryFormatter formate = new BinaryFormatter();                formate.Serialize(fs, book);            }        }        public Book DeSerialize()        {            Book book;            using (fs = new FileStream(filepath, FileMode.Open))            {                BinaryFormatter formate = new BinaryFormatter();                book = (Book)formate.Deserialize(fs);            }            return book;        }    }    class SoapSerialize : ISerialize    {        FileStream fs;        string filepath = @"D:/1/2.txt";        public void Serialize(Book book)        {            using (fs = new FileStream(filepath, FileMode.Create))            {                SoapFormatter formate = new SoapFormatter();                formate.Serialize(fs, book);            }        }        public Book DeSerialize()        {            Book book;            using (fs = new FileStream(filepath, FileMode.Open))            {                SoapFormatter formate = new SoapFormatter();                book = (Book)formate.Deserialize(fs);            }            return book;        }    }    class XMLSeria : ISerialize    {        FileStream fs;        string filepath = @"D:/1/3.txt";        public void Serialize(Book book)        {            using (fs = new FileStream(filepath, FileMode.Create))            {                XmlSerializer formate = new XmlSerializer(typeof(Book));                formate.Serialize(fs, book);            }        }        public Book DeSerialize()        {            Book book;            using (fs = new FileStream(filepath, FileMode.Open))            {                XmlSerializer formate = new XmlSerializer(typeof(Book));                book = (Book)formate.Deserialize(fs);            }            return book;        }    }    public interface BaseFacotry    {        ISerialize CreateSerialize();    }    class BinaryFactory : BaseFacotry    {        public ISerialize CreateSerialize()        {            return new BinarySerialize();        }    }    class SoapFactory : BaseFacotry    {        public ISerialize CreateSerialize()        {            return new SoapSerialize();        }    }    class XMLFactory : BaseFacotry    {        public ISerialize CreateSerialize()        {            return new XMLSeria();        }    }}
程式碼片段2:
前台調用:
    <asp:Button ID="BinaryFactory" runat="server" Text="BinarySerialize" onclick="Btn_Click" />    <asp:Button ID="SoapFactory" runat="server" Text="SoapSerialize" onclick="Btn_Click" />    <asp:Button ID="XMLFactory" runat="server" Text="XMLSerialize" onclick="Btn_Click"  />    protected void Btn_Click(object sender, EventArgs e)    {        Book book = new Book();        book.BookName = "入門經典";        book.Price = 120;        string id = ((Button)sender).ID;        BaseFacotry factory = (BaseFacotry)Assembly.Load("Define").CreateInstance("Define." + id);        ISerialize serialize = factory.CreateSerialize();        serialize.Serialize(book);        Book b = serialize.DeSerialize();        Response.Write(b.ToString());    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.