Register | Login

Answer:

According to Wikipedia serialization is the process of converting a objects into a format (sequence of bytes) that can be stored in a file, a memory, or transmitted across a network.

Solution in .NET is to utilize BinaryFormatter class from System.Runtime.Serialization.Formatters.Binary namespace (mscorlib.dll assembly) and Generics.

BinaryFormatter class implement IFormatter interface (inherited by the IRemotingFormatter) to support serialization of a graph of objects.

Include references to:


using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;



void BinarySerialize<T>(string file, T target)
{
FileStream fs = new FileStream(file, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, target);
fs.Close();
}


and for deserialization:


T BinaryDeSerialize<T>(string file)
{
FileStream fs = new FileStream(file, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
T result = (T)formatter.Deserialize(fs);
fs.Close();
return result;
}





Who Voted for this Question


Article



Common Interview is a place to help people keep up with the latest trends in job interviewing. You can interact by asking interview questions or by providing answers and ratings. Choose from thousands behavioural, technical, testing or program management questions and interview puzzles.