11

我需要对我的复杂对象模型执行深度克隆。您认为在 .Net 中做到这一点的最佳方式是什么?
我考虑过序列化/反序列化
,不用说MemberwiseClone还不够好。

4

7 回答 7

15

如果你控制了对象模型,那么你可以写代码来做,但是需要大量的维护。但是有很多问题,这意味着除非您绝对需要最快的性能,否则序列化通常是最易于管理的答案。

这是可以接受的情况BinaryFormatter之一;通常我不是粉丝(由于版本控制等问题) - 但由于序列化数据是为了立即使用,这不是问题。

如果您希望它更快一点(但没有您自己的代码),那么protobuf-net可能会有所帮助,但需要更改代码(添加必要的元数据等)。它是基于树的(不是基于图的)。

其他序列化器(XmlSerializer, DataContractSerializer)也很好,但如果它只是用于克隆,它们可能不会提供太多BinaryFormatter(除了可能XmlSerializer不需要[Serializable].

所以真的,这取决于你的确切课程和场景。

于 2009-08-09T12:42:56.253 回答
10

如果您在部分信任环境(例如 Rackspace Cloud)中运行代码,您可能会被限制使用 BinaryFormatter。可以改用 XmlSerializer。

public static T DeepClone<T>(T obj)
{
    using (var ms = new MemoryStream())
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        xs.Serialize(ms, obj);
        ms.Position = 0;

        return (T)xs.Deserialize(ms);
    }
}
于 2009-11-30T20:16:30.053 回答
5

来自 msdn 杂志的深度克隆示例:

    Object DeepClone(Object original)
    {
        // Construct a temporary memory stream
        MemoryStream stream = new MemoryStream();

        // Construct a serialization formatter that does all the hard work
        BinaryFormatter formatter = new BinaryFormatter();

        // This line is explained in the "Streaming Contexts" section
        formatter.Context = new StreamingContext(StreamingContextStates.Clone);

        // Serialize the object graph into the memory stream
        formatter.Serialize(stream, original);

        // Seek back to the start of the memory stream before deserializing
        stream.Position = 0;

        // Deserialize the graph into a new set of objects
        // and return the root of the graph (deep copy) to the caller
        return (formatter.Deserialize(stream));
    }
于 2009-08-09T22:06:25.993 回答
1

请查看非常好的文章C# Object Clone Wars。我在那里找到了一个非常有趣的解决方案:Copyable: A framework for copying or cloning .NET objects

于 2011-06-06T15:57:45.440 回答
0

您可以尝试AltSerialize,它在许多情况下比 .Net 序列化程序更快。它还提供缓存和自定义属性来加速序列化。

于 2009-08-09T20:55:46.883 回答
0

最好的方法可能是在您的对象及其所有需要自定义深度克隆功能的字段中实现 System.IClonable 接口。然后实现Clone方法以返回对象及其成员的深层副本。

于 2009-08-09T12:22:40.693 回答
0

手动实现此功能的最佳方法。这将比任何其他通用方法都快。此外,还有很多用于此操作的库(您可以在此处查看一些带有性能基准的列表)。

顺便说一句,BinaryFormatter 对于这项任务来说非常慢,并且只能用于测试。

于 2016-11-01T11:37:08.870 回答