This document discusses serialization in C# and how it allows objects to be written to and read from files and databases. Serialization converts an object into a stream of bytes that can be saved. It involves making a class serializable, using a stream to serialize the object, and a formatter. The example provided uses the BinaryFormatter to serialize a Worker object with name and phone properties to a file, and then deserialize it back into an object from the file. Serialization interfaces like ISerializable are implemented to get and set property values during serialization and deserialization.
1. Serialization using C#
By Salman Mushtaq Page 1
Serialization
using C#
December 2
2015
When we want to write or read data from files and database, we do not do
this directly. Because object is not automatically converted. Casting is not
useful here. So, that’s why we use serialization for doing it correctly.
Reading &
Writing
objects using
serialization
2. Serialization using C#
By Salman Mushtaq Page 2
Asalam U Alaikum all readers!
I am Salman and today we discuss about Serialization in C#.
Suppose we have a small project. In project you develop a module and you need to save objects into
files. What can you do? Exactly you use some classes of System.IO like FileStream, StreamWriter and
StreamReader.
So, you make an object and write it to file. But when you see the file data it just Namespace.FileName.
You think what the wrong here is. Similarly when you try to read data you receive exception.
So here we need serialization. Serialization is used where we need to write object to database or file.
And vice versa. See the below diagram.
According to diagram. We have object and with the help of serialization object converts into bytes then
it saves to memory, database or file.
Now what we need to do all this.
- Class/Object for serialization
- A stream to serialized object
- Formatter
- Namespace: System.Runtime.Serialization
- Interface: ISerializable
Now, we write some code and explain it to you.
See code on start on next page.
3. Serialization using C#
By Salman Mushtaq Page 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace TestObjectWritingInTextfile
{
[Serializable()]
class Worker : ISerializable
{
public string name;
public string phone;
// Default constructor
public Worker()
{
name = null;
phone = null;
}
// Parameterized constructor
public Worker(string name, string phone)
{
this.name = name;
this.phone = phone;
}
public Worker(SerializationInfo info, StreamingContext ctxt)
{
name = (string)info.GetValue("Name", typeof(string));
phone = (string)info.GetValue("Phone", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Name", name);
info.AddValue("Phone", phone);
}
}
public class Contact
{
public static void Main(String[] args)
{
Worker obj = new Worker("Salman Mushtaq", "03337465571");
//For writing data
Stream sw = File.Open(@"C:UsersManiDesktopFilesnames.txt",
FileMode.Append);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(sw, obj);
sw.Close();
//For reading data
4. Serialization using C#
By Salman Mushtaq Page 4
Stream sr = File.Open(@"C:UsersManiDesktopFilesnames.txt",
FileMode.Open);
BinaryFormatter br = new BinaryFormatter();
obj = (Worker)br.Deserialize(sr);
sr.Close();
Console.WriteLine("Name: " + obj.name);
Console.WriteLine("Name: " + obj.phone);
Console.ReadKey();
}
}
}
You need three namespaces:
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
First of all you must implement ISerializable interface. It have two function one for Serialization and
second is for De-serialization. Both function have two parameters.
SerializationInfo info, StreamingContext ctxt
Method GetObjectdata is used for writing data. As we know we first convert all data into
bytes so we need formatter. That help us to write or read data on stream.
Hope you understand. If you have some question please email me at salman_1538@hotmail.com