As programmers, we use serialization for storage of classes and data structures. With the advent of .NET, serialization is becoming something like a standard and there are fundamental reasons for that - .NET has everything for serialization (well almost). Typically the serialization process consists of creation of the serializer, opening of the stream and invocation of the serializer. You do that again and again and eventually realize that there is a pattern which might be hidden inside some generic class.
There are many articles about serialization in .NET last time. Some of them describe basic operations with serialization, some more advanced describe the classes for serialization. (like Thurston Cummins http://www.codeproject.com/useritems/XML_Serializationasp.asp).
This article is another approach to create a generic class for the serialization. The goal : the class should serialize itself, to be self contained, be simple. Generally this class should looks like :
Class GSerialized : BaseSerializable
{
// some custom stuff
int MyVarInt1;
int MyVarInt2;
ArrayList MyArrayList1;
// and so on
}
In other words the class has to be derived from some generic class that implements basic serialization.
Another solution is the implementation of the methods of Serializable interface. Both approaches have pros and cones.
Methods for implementing Serializable interface are well explained in different articles and manuals. From my point of view this approach is more complicated and therefore less elegant.
The solution I am proposing is the creation of an abstract serialization class. The abstract class named BaseSerializable has basic functionality to serialize and deserialize itself.
However the implementation of BaseSerializable is not entirely generic due to the limitations of C# . Namely :
In the constructor of derived class the protected variable ChildObjectRef must set to this. That is the only flaw of C# implementation of the generic BaseSerializable class.
(In C++ that could have been implemented in entirely generic manner through template mechanism)
//BaseSerializable class
using System;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization;
using System.IO;
namespace UtilLib
{
[Serializable]
public abstract class BaseSerializable
{
protected object ChildObjectRef;
public BaseSerializable()
{}
public void _Serialize(string FullPath)
{
if (FullPath == null)
{
throw new Exception("_Serialize: Path not set");
}
string DirName = Path.GetDirectoryName(FullPath);
FileStream s = null;
if (!((DirName == null) || (DirName.Trim().Length == 0)))
{
if (!Directory.Exists(DirName))
{
Directory.CreateDirectory(DirName);
}
}
s = new FileStream(FullPath, FileMode.Create);
SoapFormatter sf = new SoapFormatter();
sf.Serialize(s, ChildObjectRef);
s.Close();
}
public object _Deserialize(string FullPath)
{
FileStream s = new FileStream(FullPath, FileMode.Open);
SoapFormatter sf = new SoapFormatter();
object msc = (object)sf.Deserialize(s);
s.Close();
return msc;
}
}
}
How to create your class?
Your serializable class will look like the class below:
using System;
using UtilLib;
using System.Collections;
[Serializable]
public class MySerialClass : BaseSerializable
{
public int MyDummyInteger; // could be private
public string MyDummySting; // could be private ..
public ArrayList MyDummyArrayList; // or another serializable //object
public MySerialClass ()
{
ChildObjectRef = this; // that is mandatory !!
}
// My methods below, if any
}
The only condition is that ChildObjectRef variable inherited from the base class must be set before serialization. It would be logical to set it in the constructor.
How to use the class?
Here is a code snippet:
MySerialClass MyDataKeeper_1 = new MySerialClass();
MyDataKeeper_1.MyDummySting = "Dummy_String";
MyDataKeeper_1.MyDummyInteger = 4545;
ArrayList al = new ArrayList();
al.add(hello);
al.add(by);
MyDataKeeper_1. MyDummyArrayList = al;
MyDataKeeper_1._Serialize("test.xml");
// deserialisation
MySerialClass MyDataKeeper_2 = new MySerialClass();
Try
{
MyDataKeeper_2 = (MySerialClass)MyDataKeeper_2._Deserialize("test.xml");
}
catch
{
// your code if deserealization fails for any reason.
}