Serialization is one of the concepts, that is always intriguing to developers.
One of my coworker, was trying to move the in process session state management into an out of proc session state service. We were getting some exceptions during the transition, of course when objects need to be persisted in a session, they have to serializable and more than that, if it is an aggregated object (object which may have multiple objects encapsulated in it) then a null check should be performed on each object in the serialization / deserialization method definitions.
VB.NET
Public Overrides Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
If Not IsNothing(_empobject) Then
info.AddValue("_empobject", _empobject.Serialize(_empobject))
End If
End sub
Private Sub New(ByVal Info As SerializationInfo, ByVal Context As StreamingContext)
If Not String.IsNullOrEmpty(Info.GetString("_empobject")) Then
_empobject = New EmpObject().DeSerialize(Info.GetString("_empobject"))
End If
End Sub
C#
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
if ((_empobject != null)) {
info.AddValue("_empobject", _empobject.Serialize(_empobject));
}
}
private New(SerializationInfo Info, StreamingContext Context)
{
if (!string.IsNullOrEmpty(Info.GetString("_empobject"))) {
_empobject = EmpObject().DeSerialize(Info.GetString("_empobject"));
}
}
Finally, how do you serialize a generic list ( of any Type). I was thinking about that, and found this article which explains clearly how to do it.
http://www.eggheadcafe.com/software/aspnet/30067316/how-can-i-add-generic-lis.aspx
If you wonder how to xml serialize a generic list, look at the below blog post
http://www.hanselman.com/blog/XmlSerializingAGenericListltgt.aspx
If you want a tool to convert a code from VB to C# , you could use the below link to do that
http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx