Ehsan Ghanbari

Experience, DotNet, Solutions

Working with serialization in C#

Do you know how .Net mechanism stores your object in memory? I don't know! But we should know how to store the content of an object into a file (in the simplest form) or transfer the object between a network, passing an object from one domain to another, sending the object to a remote application by means of a Web Service , Rest, and so on. To do more security you should convert the content into different format and after the operation re-convert it to the same format. This operation is called Serialization. Sterilization in an operation defined in .Net, it's all about transforming the state of an object into serial data like XML, binary or another format and re-store or recreate it when needed (this recreation operand is deserialization).

Note: writing the plain text into a file or transferring it is so dangerous, that's because we serial the content to make it more secure.

Let's take an example, in .Net the namespace System.Runtime.Serialization contains some members to do this. I'm just going to make an example on writing content and serializing it into a file because it is the simplest one!

 Create a class with this definition:

  1.     [Serializable]
  2.     public class Content : ISerializable
  3.     {
  4.         public string Name { get; set; }
  5.         public DateTime DateTime { get; set; }
  6.  
  7.         public Content()
  8.         {
  9.         }
  10.  
  11.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  12.         {
  13.             info.AddValue("Ehsan", Name);
  14.             info.AddValue("10/18/2013", DateTime);
  15.         }
  16.     }

 Iserializable interface is for controlling the serialization process and if you want to serialize a content you must apply the Serializable attribute. You can see the controlling the serialization in GetObjectData().

 Now add the Serialize and Deserialize methods to complete the operation

  1.   public class Presentaion
  2.     {
  3.         public static void Serialize()
  4.         {
  5.             var content = new Content {Name = "Ehsan", DateTime = DateTime.Now};
  6.             Stream stream = File.Open("MyFile", FileMode.Create);
  7.             var binaryFormatter = new BinaryFormatter();   //System.Runtime.Serialization
  8.             binaryFormatter.Serialize(stream,content);
  9.  
  10.         }
  11.  
  12.         public static void Deserialize()
  13.         {
  14.             var content = new Content();
  15.             Stream stream = File.Open("MyFile", FileMode.Open);
  16.             var binaryFormatter = new BinaryFormatter();
  17.             content = (Content) binaryFormatter.Deserialize(stream);
  18.             stream.Close();
  19.  
  20.             //Operation on content
  21.         } 
  22.     }

 I just create the file, serialized the content in Serialize() and then de-serialized it in Deserialize().

 there are some notes about serialization you should know about:

  1. It is important to note that the Serializable attribute cannot be inherited.
  2. serialization cannot be added to a class after it has been compiled.
  3. It is important to use properties, which have the get and set keywords, in this sort of serialization code.

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in asp.net MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In asp.net MVC
Multiple submit buttons in asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus