Wednesday, April 15, 2009

Serialization

[Serializable]


Serialization is writing an object as stream.Think you have a class which you need to save somewhere to disk to a database serialization makes it easy

Lets make an example:

Start by writing [Serializable]  over your class and derive your class from ISerializable we need a constructor to convert from serialization to our class in this example

public Color(SerializationInfo info, StreamingContext context)

and also you need GetObjectData Method which serializes the object .I think the example is very self explaining.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.ComponentModel;
   4: using System.Data;
   5: using System.Drawing;
   6: using System.Linq;
   7: using System.Text;
   8: using System.IO;
   9: using System.Windows.Forms;
  10: using System.Runtime.InteropServices;
  11: using System.Runtime.Serialization;
  12: using System.Runtime.Serialization.Formatters.Binary;
  13:  
  14:  
  15: namespace Master
  16: {
  17:     public partial class Form1 : Form
  18:     {
  19:         public Form1()
  20:         {
  21:             Color testColor = new Color(23, 23, 23);
  22:             MemoryStream ms = new MemoryStream(200);
  23:             BinaryFormatter bf = new BinaryFormatter();
  24:             bf.Serialize(ms, testColor);
  25:             ms.Position = 0;
  26:             Color verificationColor =(Color)bf.Deserialize(ms);
  27:  
  28:         }
  29:     }
  30:     [Serializable]
  31:     public class Color : ISerializable
  32:     {    
  33:         private byte red;
  34:         private byte green;
  35:         private byte blue;
  36:         public byte Red
  37:         {
  38:             get
  39:             {
  40:                 return red;
  41:             }
  42:             set
  43:             {
  44:                 red = value;
  45:             }
  46:         }
  47:         public byte Green
  48:         {
  49:             get
  50:             {
  51:                 return green;
  52:             }
  53:             set
  54:             {
  55:                 green = value;
  56:             }
  57:         }
  58:         public byte Blue
  59:         {
  60:             get
  61:             {
  62:                 return blue;
  63:             }
  64:             set
  65:             {
  66:                 blue = value;
  67:             }
  68:         }
  69:         public Color()
  70:         {
  71:             red = 0;
  72:             green = 0;
  73:             blue = 0;
  74:         }
  75:         public Color(byte r, byte g, byte b)
  76:         {
  77:             Red = r;
  78:             Green = g;
  79:             Blue = b;
  80:         }
  81:  
  82:         public Color(SerializationInfo info, StreamingContext context)
  83:         {
  84:             Red = (byte)info.GetValue("RED", typeof(byte));
  85:             Green = (byte)info.GetValue("GREEN", typeof(byte));
  86:             Blue = (byte)info.GetValue("BLUE", typeof(byte));
  87:         }
  88:  
  89:         #region ISerializable Members
  90:  
  91:         public void GetObjectData(SerializationInfo info, StreamingContext context)
  92:         {
  93:             info.AddValue("RED", Red);
  94:             info.AddValue("GREEN", Green);
  95:             info.AddValue("BLUE", Blue);
  96:         }
  97:  
  98:         #endregion
  99:     }
 100:  
 101: }

No comments: