What is Binary Serialization in .NET?
In simple terms, Serialization is the process of saving and then loading an object back into memory. Objects are serialized to a file rather than a database so are personal based. There are three types of Serialization, they are :-
- Binary
- XML
- Soap
This webpage deals only with Binary Serialization. Before you can even start using serialization, you need to add the respective namespaces which are :-
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
Unlike XML Serialization, the file is not in easy to read human format. All classes that are to be serialized must be identified by a Serializable tag as below.
[Serializable]
class clsPerson
{
public clsPerson(string strForename, string strSurname)
{
Forename = strForename;
Surname = strSurname;
}
[OnDeserialized]
public void DisplayName(System.Runtime.Serialization.StreamingContext context)
{
Console.WriteLine("Hello {0} {1}", Forename, Surname);
}
public string Forename { get; set; }
public string Surname { get; set; }
[OptionalField(VersionAdded = 2)]
string m_Department;
public string Department
{
get { return m_Department; }
set { m_Department = value; }
}
}
In the above example, you will have noticed [OnDeserialized], this means that that function will automatically be called once the object has been deserialized from the database. The other qualifiers are :-
Qualifier | Explanation |
[OnSerializing] | Function called before the Serialization has taken place. |
[OnSerialized] | Function called after the Serialization has taken place. |
[OnDeserializing] | Function called before the Deerialization has taken place. |
[OnDeserialized] | Function called after the Deserialization has taken place. |
[OptionalField] | This allows you to deserialize an older version of serialised data into a newer version of the object without throwing an error. You cannot use this attribute on autoimplemented properties, you must define the variable and attribute the long way. |
[OnDeserialized] | Function called after the Deserialization has taken place. |
[NonSerialized] | The field will not be serialized. |
Now comes the fun part, the serialization which is really easy. In short, you define the formatter then write the object to a stream. If you looked at the file that is created, you will find the information is relatively easy to understand.
class Program
{
static void Main(string[] args)
{
//Create Instance of object to be serialized.
clsPerson Person = new clsPerson("Mister","Developer");
// Create serializer that will do the work.
BinaryFormatter binFormat = new BinaryFormatter();
// Open a stream where the serialized file will be stored.
using(Stream fptr = new FileStream("C:\\temp\\user.dat", FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fptr,Person);
}
Person = null;
// Deserialize the object.
using (Stream fptr = File.OpenRead("c:\\temp\\user.dat"))
{
// As we have an [OnDeserialized] function, the forename + surname will be displayed on screen.
Person = (clsPerson)binFormat.Deserialize(fptr);
}
Console.ReadKey();
}
}
Tags - .NET Serialization
Last Modified : June 2023