深度复制有多深? [英] How deep is deep copy?

查看:97
本文介绍了深度复制有多深?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,我想复制一个对象的深层副本.但是到底有多深呢?
例如,我有一个SalesOrder类.我可以通过创建一个新的SalesOrder对象并将其复制到新的Object中来进行深度复制.但是,SalesOrder具有SalesOrderDetails的集合.幸运的是,SalesOrderDetail类没有DeepClone方法,因此我将不得不复制SalesOrder中的每个SalesOrderDetail对象...这是有趣的部分,每个SalesOrderDetail都有一个产品,而该产品也没有DeepClone方法(是否有任何具有此方法的.NET类?).
例如,在Entity Framework的情况下,每个产品都有一个SalesOrderDetails列表,这些列表又有一个SalesOrder,并且圆是完整的... Entity Framework使用延迟加载不会立即将整个数据库加载到您的应用程序中.但是,当您要克隆时,实际上需要调用每个对象,以防它存在并且需要被克隆.

我到处搜索Google,并且看到很多人说序列化是创建对象深层副本的最简单方法,但是我的经验是,许多(非定制的)类不能像那样(序列化)进行序列化引用等).如果它们可以完全序列化的话.

那么,深层副本实际上应该走多深,甚至有可能为更复杂的对象创建完美的深层副本?我想反射可能会让您走得很远,但这也远非理想(尽管对我而言,优先于序列化).

有什么想法吗?
谢谢.

In .NET I want to make a deep copy of an Object. But how deep is exactly deep?
For example, I have a SalesOrder Class. I could deep copy it by creating a new SalesOrder Object and copy all value types into the new Object. However, a SalesOrder has a collection of SalesOrderDetails. Luck has it that the SalesOrderDetail Class does not have a DeepClone Method, so I will have to copy every SalesOrderDetail Object in the SalesOrder... Here comes the fun part, every SalesOrderDetail has a Product, which also does not have a DeepClone Method (are there ANY .NET Classes that have this Method?).
In the case of, for example, Entity Framework every Product has a list of SalesOrderDetails which again have a SalesOrder and the circle is complete... Entity Framework uses lazy loading to not load an entire database into your application at once. But when you''re going to clone you effectively need to call every Object in case it exists and needs to be cloned.

I''ve Googled around and I''ve seen many people saying Serialization is the easiest way to create a deep copy of an Object, but my experience is that many (non-custom made) Classes cannot be Serialized just like that (circular references etc.) if they can be serialized at all.

So how deep should a deep copy actually go and is it even possible to create a perfect deep copy of a more complex Object? I guess Reflection might take you pretty far, but this too is far from ideal (although preferred above Serialization for me).

Any thoughts?
Thanks.

推荐答案

这完全取决于您的需求.您不会仅仅为了复制它而复制任何东西.相反,您肯定有使用所需副本的要求.此要求决定了副本中应包含的内容.

序列化还不错.如果您不使用XMLSerializer,它可以很好地处理循环引用.坚持使用BinaryFormatterSOAPFormatter.但是要注意.以最小可能的编码量使用它们会复制与对象有关的所有内容.您必须小心地排除事物,这又导致了上述要求.
It totally depends on your needs. You won''t copy anything just for the sake of copying it. Instead you definitely have a requirement that uses desired copy. This requirement dictates what should be included in your copy.

Serialization is not that bad. It can handle circular references pretty well, if you refrain from using XMLSerializer. Stick to BinaryFormatter or SOAPFormatter instead. But be warned. Using them in a least-possible-amount-of-coding approach will copy everything related to your object. You have to carefully exclude things, which again leads to the requirement mentioned above.


取决于您的序列化程序,它的深度将与序列化程序的深度相同.

例如,如果您使用Binaryformater,它将创建所有引用对象的副本,包括循环引用.

对于其他序列化程序,通常会在一定深度处忽略或破坏循环引用(在我的fastJSON序列化程序中,我已将其设置为10深)
Depending on your serializer it will go as deep as the serializer goes.

For example if you are using the Binaryformater it will create a copy of all the objects referenced, including circular references.

For other serializers usually circular references are ignored or broken out of at a certain depth (in my fastJSON serializer I have set this to 10 deep)


这篇关于深度复制有多深?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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