在Silverlight中克隆業務對象的最佳方法是什么?

[英]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?

創建DTO克隆的最佳方法是什么? Silverlight中沒有ICloneable接口或BinaryFormatter類。反思是唯一的方法嗎?

4 个解决方案

#1


9  

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

這是我們為克隆提出的代碼。這適用於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

#2


3  

ICloneable is not available in Silverlight 4(I don't know about 1/2/3 or upcoming version) . It is removed from Silverlight 4's public APIs. Help from Mike Schall code; it is working for me.

Silverlight 4中沒有ICloneable(我不知道1/2/3或即將推出的版本)。它已從Silverlight 4的公共API中刪除。來自Mike Schall代碼的幫助;它對我有用。

public LayerDto Clone(LayerDto source)
    {

        DataContractSerializer serializer = new DataContractSerializer(typeof(LayerDto));
        using (MemoryStream ms = new MemoryStream())
        {
            serializer.WriteObject(ms, source);
            ms.Seek(0, SeekOrigin.Begin);
            return (LayerDto)serializer.ReadObject(ms);
        }
    }

#3


1  

How to create clone if my source is IEnumerable. This LayerDto also has some object type(eg MetaItemDto).

如果我的源是IEnumerable,如何創建克隆。這個LayerDto也有一些對象類型(例如MetaItemDto)。

Code :

public class LayerDto { }
public class MetaItemDtoList : System.Collections.ObjectModel.ObservableCollection { }

public class LayerDto {} public class MetaItemDtoList:System.Collections.ObjectModel.ObservableCollection {}

public static IEnumerable Clone(IEnumerable source)

public static IEnumerable Clone(IEnumerable source)

{

        IEnumerable<LayerDto> layers;

        DataContractSerializer serializer = new DataContractSerializer(typeof(IEnumerable<LayerDto>));
        using (MemoryStream ms = new MemoryStream())
        {
            serializer.WriteObject(ms, source);
            ms.Seek(0, SeekOrigin.Begin);
            //return (IEnumerable<LayerDto>)serializer.ReadObject(ms);
            layers = (IEnumerable<LayerDto>)serializer.ReadObject(ms);
            return layers;
        }

}

but what is problem I am facing is that layer doesn't show it's metaItems(for every layer).

但是我面臨的問題是圖層沒有顯示它的metaItems(對於每一層)。

#4


0  

I believe the standard cloning functionality was left out to keep it simple and lightweight. I believe you could use either JSON or XML serialization to achieve the same thing though. Not sure about the performance costs though.

我相信標准的克隆功能是為了保持簡單和輕量級。我相信你可以使用JSON或XML序列化來實現同樣的目的。但不確定性能成本。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/10/14/724f9223c9e57a881fbf3fecd4b718f2.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com