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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With