在Silverlight中克隆业务对象的最佳方法是什么?

在Silverlight中克隆业务对象的最佳方法是什么?

问题描述:

创建DTO克隆的最佳方法是什么? Silverlight中没有ICloneable接口或BinaryFormatter类.反射是唯一的方法吗?

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?

这是我们提供的用于克隆的代码.这适用于Silverlight 2& 3.

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