Object Clone using Serialization in ASP.NET

Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
 
Function ObjectCopy(ByVal obj As Object) As Object
        Dim objMemStream As New MemoryStream(5000)
        Dim objBinaryFormatter As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))
        objBinaryFormatter.Serialize(objMemStream, obj)
        objMemStream.Seek(0, SeekOrigin.Begin)
        ObjectCopy = objBinaryFormatter.Deserialize(objMemStream)
        objMemStream.Close()
End Function

2 comments: