การทำ Serialization และ Deserialization ใน C#

by m3rLinEz 25. January 2009 23:13

เป็นหัวข้อน่ารู้อีกอย่างนึงครับ สำหรับเรื่องของ Serialization และ Deserialization

ตามนิยาม (ในวิกิพีเดีย) เค้าบอกไว้ว่า

Serilization คือกระบวนการแปลงวัตถุให้กลายเป็นสายข้อมูลในรูปแบบบิต ทำให้สามารถเก็บรักษาวัตถุดังกล่าวไว้บนสื่อเก็บข้อมูล (เช่น HDD) หรือจะเอาไปส่งผ่าน Network ก็ได้

Deserilization คือกระบวนการย้อนกลับของ Serialization คือการแปลงจากสายข้อมูลในรูปแบบบิตให้กลับมาเป็นวัตถุของเรานั่นเอง

จากรูป วัตถุ "ปลาดิบ" จะถูกแปลงเป็นข้อมูลในรูปแบบ Binary เพื่อเก็บไว้บนดิสค์ครับ และหากต้องการนำมาใช้ใหม่ก็แค่โหลดข้อมูลขึ้นมาทำการ Deserialzation

จริงๆแล้วขั้นตอนการเก็บรักษา "สถานะ" ของวัตถุตามที่เห็นในรูป มันก็สามารถทำมือได้เช่นกัน โดยอาจจะเขียนข้อมูลต่างๆใส่ Text File แล้วเขียนลอจิกที่ทำหน้าที่โหลดข้อมูลดังกล่าวขึ้นมาเอง แต่ตามความเข้าใจของผม วิธีการนี้ก็เป็นวิธีการหนึ่งในการเก็บรักษาสภาพของวัตถุที่สะดวกดีครับเขียนเพิ่มนิดหน่อยก็ทำ Serialization ได้เลย

วิธีการทำใน C# มีขั้นตอนดังต่อไปนี้ ยกตัวอย่างจากคลาสที่เขียนในวันนี้

  1. เพิ่ม Attribute Serializable เข้าไปที่หัว Class ก่อน
  2. ให้ Class implements ISerializable ต้องเขียน method GetObjectData เพิ่ม
  3. ทำ Deserialization Constructor ที่มีรูปแบบตามที่กำหนด
  4. เพิ่มลอจิกส่วนที่เป็นการทำ Serialize และ Deserialize

ดูโค้ดดีกว่า

ส่วนของ ToBinaryFile และ FromBinaryFile เป็นส่วนของข้อ 4 นะครับ

เวลาจะทำ Serialize ก็เรียก ToBinaryFile("filename.osl") และเวลาจะ Deserialize ก็เรียก Word.FromBinaryFile("filename.osl")

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

namespace WordParser
{
    [Serializable()]
    class Word : ISerializable
    {
        public string Value { get; set; }
        public string Description { get; set; }
        public int Popularity { get; set; }

        public Word()
        {

        }
       
        public Word(SerializationInfo info, StreamingContext context)
        {
            Value = (string)info.GetValue("Value", typeof(string));
            Popularity = (int)info.GetValue("Popularity", typeof(int));
            Description = (string)info.GetValue("Description", typeof(string));
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Value", Value);
            info.AddValue("Popularity", Popularity);
            info.AddValue("Description", Description);
        }

        #endregion

        public static Word FromBinaryFile(string file)
        {
            using (Stream stream = File.Open(file, FileMode.Open))
            {
                BinaryFormatter bformatter = new BinaryFormatter();
                Word ret = (Word)bformatter.Deserialize(stream);
                return ret;
            }

           
        }

        public void ToBinaryFile(string file)
        {
            using (Stream stream = File.Open(file, FileMode.Create))
            {
                BinaryFormatter bformatter = new BinaryFormatter();
                bformatter.Serialize(stream, this);
            }

        }

    }
}

Tags: , , Category: .NET | OOAD

Comments (3) -

teerapap
teerapap
1/26/2009 2:21:44 AM #

ในที่ทำ robocup ก็มีใช้นะ สะดวกดี
Serialize/Deserialize จาก XML เป็น ค่า config โดยทำเป็น Wrapper Class

กร
กร
1/28/2009 8:35:04 PM #

ทำไมไม่เขียนเป็นภาษาอังกฤษที่อีกบล็อกนึงหละ ไม่งั้นคนอินเดียที่ไหนมันจะมาอ่าน

m3rlinez
m3rlinez
1/28/2009 9:13:12 PM #

ฮ่าๆ เรื่องนี้มันง่ายไป คนเขียนภาษาอังกฤษกันเกลื่อนแล้ว เอาภาษาไทยละกัน Laughing

Add comment




biuquote
  • Comment
  • Preview
Loading






Most comments

khimkhim khimkhim
1 comments
weaw weaw
1 comments
domehuhu domehuhu
1 comments

RecentComments

Comment RSS