Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to clone a business object in Silverlight?

What is the best way to create a clone of a DTO? There is not an ICloneable interface or a BinaryFormatter class in Silverlight. Is reflection the only way?

like image 859
Mike Schall Avatar asked Oct 14 '08 20:10

Mike Schall


1 Answers

Here is the code we came up with for cloning. This works in Silverlight 2 & 3.

Public Shared Function Clone(Of T)(ByVal source As T) As T
    Dim serializer As New DataContractSerializer(GetType(T))
    Using ms As New MemoryStream
        serializer.WriteObject(ms, source)
        ms.Seek(0, SeekOrigin.Begin)
        Return DirectCast(serializer.ReadObject(ms), T)
    End Using
End Function
like image 192
Mike Schall Avatar answered Oct 01 '22 17:10

Mike Schall