为什么不.NET框架提供了一种方法,以深拷贝的对象? [英] Why doesn't the .NET framework provide a method to deep copy objects?

查看:135
本文介绍了为什么不.NET框架提供了一种方法,以深拷贝的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的方法来深克隆对象的一天,我知道你可以在不同的方法(反射,二进制序列化等)深克隆,所以我只是想知道:

I was using a custom method to deep clone objects the other day, and I know you can deep clone in different ways (reflection, binary serialization, etc), so I was just wondering:

什么是/是的原因(S),微软并不包括在该框架的深层副本的方法?

What is/are the reason(s) that Microsoft does not include a deep copy method in the framework?

推荐答案

的问题基本上是难度比你似乎意识到,至少在一般情况下。

The problem is substantially harder than you seem to realize, at least in the general case.

对于初学者来说,副本不只是深或浅,这是一个频谱。

For starters, a copy isn't just deep or shallow, it's a spectrum.

让我们想象一下,一个第二,我们有字符串数组列表,并且我们要它的一个副本。

Let's imagine for a second that we have a list of arrays of strings, and we want to make a copy of it.

  • 我们开始了在最浅的层面,我们只是整个事情的引用复制到另一个变量。无论从变量引用任何更改名单由其他可见。

  • We start out at the shallowest level, we just copy the reference of the whole thing to another variable. Any changes to the list referenced from either variable is seen by the other.

所以,现在我们去创造一个全新的列表,给第二个变量。对于在第一列表中的每个项目,我们把它添加到第二列表。现在,我们可以通过修改从任何变量引用没有它被看见的另一个列表。但是,如果我们抓住一个列表的第一个项目并改变字符串的第一个数组中,它将被两个列表可见一斑!

So now we go and create a brand new list to give to the second variable. For each item in the first list we add it to the second list. Now we can modify the list referenced from either variable without it being seen by the other one. But what if we grab the first item of a list and change the string that's in the first array, it will be seen by both lists!

现在我们正在经历,并创建一个新的列表,并为我们创建一个新的数组的第一个列表中的每个阵列,新阵列添加每个字符串的基础数组中,并加入每一个这些新的阵列到新名单。现在我们可以突变任何任一列表中的数组没有看到变化。别急,这两个名单仍然引用相同的字符串(这毕竟是值类型,它们内部有一个字符数组为他们的数据)。如果有些意思的人都一起去,并发生变异的字符串(使用不安全code,你可以真正做到这一点)之一!所以,现在你复制所有串了深刻的副本。但是,如果我们不需要做呢?如果我们知道没有人是这样的意思,他们会变异的串?或者,对于这个问题,如果我们知道,没有一个阵列将被突变(或者,如果他们将是,他们是的应该的由两个列表体现)。

Now we're going through and creating a new list, and for each array in the first list we're creating a new array, adding each of the strings in the underlying array to the new array, and adding each of those new arrays to the new list. Now we can mutate any of the arrays in either list without seeing the changes. But wait, both lists are still referencing the same strings (which are value types after all; they internally have a character array for their data). What if some mean person were to come along and mutate one of the strings (using unsafe code you could actually do this)! So now you're copying all of the strings with a deep copy. But what if we don't need to do that? What if we know that nobody is so mean that they would mutate the string? Or, for that matter, what if we know that none of the arrays will be mutated (or that if they will be, that they're supposed to be reflected by both lists).

然后当然也有问题,如循环引用,在类的字段不这样做的真正的再present它的状态(即可能是缓存值或派生的数据可能只是被重新-calculated按需由一个克隆)。

Then of course there are problems such as circular references, fields in a class that don't really represent it's state (i.e. possibly cached values or derived data that could just be re-calculated as-needed by a clone).

实事求是你需要拥有各类实施 IClonable 或者一些等价的,有它自己的定制$ C $下如何克隆自己。这将是工作的很多的保持一种语言,特别是因为有一些复杂的物体也可能会被克隆这么多的方法。该成本会相当高,而且收益(极少数,这被认为是值得推行的克隆方法的对象外)一般都是不值得的。你,作为一个程序员,写你自己的逻辑进行克隆类型根据您有多深知道你需要去。

Realistically you'd need to have every type implement IClonable or some equivalent, and have it's own custom code for how to clone itself. This would be a lot of work to maintain for a language, especially since there are so many ways that complex objects could possibly be cloned. The cost would be quite high, and the benefits (outside of a handful of objects that it is deemed worthwhile to implement clone methods for) are generally not worth it. You, as a programmer, and write your own logic for cloning a type based on how deep you know you need to go.

这篇关于为什么不.NET框架提供了一种方法,以深拷贝的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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