在Silverlight中克隆业务对象的最佳方法是什么? [英] What is the best way to clone a business object in Silverlight?

查看:67
本文介绍了在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

这篇关于在Silverlight中克隆业务对象的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆