August 2007 - Posts

A generic class for serialization

I developed today a very simple class for serialization with generics. It supports saving serialized objects in a DB.This is code:

Public Class SerializerDeserializer(Of T)


        Public Function Serialize(ByVal obj As T) As Byte()
            Dim objMemStream As MemoryStream = New MemoryStream
            Dim binFormatter As IFormatter = New BinaryFormatter
            binFormatter.Serialize(objMemStream, obj)
            Dim data() As Byte = objMemStream.ToArray()
            Return data
        End Function

        Public Function Deserialize(ByVal data() As Byte) As T
            Dim objMemStream As Stream = New MemoryStream
            objMemStream.Write(data, 0, data.Length)
            objMemStream.Position = 0
            Dim binFormatter As IFormatter = New BinaryFormatter
            Dim obj As T = CType(binFormatter.Deserialize(objMemStream), T)
            Return obj
        End Function


    End Class

 

Bye.

Antonio 

Posted 01 August 2007 07:52 AM by antrad | no comments
Filed under:

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Breaking News
  • Find a Job
  • Social Bookmarking
    Tidebuy Reviews
    Online Shopping
    asp.net hosting
    UK online local dating