更快的深克隆 [英] Faster deep cloning

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

问题描述

有没有人想要一个框架/班,让我用值.NET对象克隆?我只能用公共读/写属性(即DataContracts)感兴趣,我不在乎,如果引用正确地解析(即collecions其中包含项目的同一个实例两次)。

Does anyone want a framework/class which allows me to clone by values .Net objects? I'm only interested with public read/write properties (namely DataContracts), and I don't care if references are resolved correctly (i.e. collecions which contains the same instance of item twice).

我试图序列化把戏通过的DataContractSerializer (序列化到XML和背部),写了反射克隆类(有时更快/有时慢),并想知道如果有人写一个辅助类,可通过放射和不反射做到这一点。至于现在发射IL是有点太多了我的小脑袋,但我想这将是最终的解决方案。除非有人知道这是比的DataContractSerializer更快的一种替代方法。

I tried serialization trick via DataContractSerializer (serialize to XML and back), wrote reflection-based cloning class (sometimes faster/sometimes slower), and was wondering if someone wrote a helper class which can do this via Emit and not reflection. As for now emitting IL is a little to much for my little brain, but I guess this would be the ultimate solution. Unless someone knows an alternative method which is faster than DataContractSerializer.

非常感谢, 卡罗尔

推荐答案

如果你正在谈论的对象树/图:

If you are talking about an object tree/graph:

编写特定的IL序列化对象是棘手。国际海事组织,最好的办法是看一个完整的序列化,怎么样的DataContractSerializer 将工作 - 但并不一定与该发动机

Writing specific IL to serialize an object is tricky. IMO, your best bet is to look at a full serialization, like how DataContractSerializer would work - but not necessarily with that engine.

例如, protobuf网有一个 Serializer.DeepClone&LT ; T> ,这可能有助于方法。它应该比的DataContractSerializer 更快,至少。在当前的时间,你需要添加一些线索串行器(哪怕只是 [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)] ) - 但是,目前的(不完全)工作正在进行中提供POCO支持无属性。

For example, protobuf-net has a Serializer.DeepClone<T> method that might help. It should be faster than DataContractSerializer, at least. At the current time, you need to add some clues for the serializer (even if just [ProtoContract(ImplicitFields=ImplicitFields.AllPublic)]) - however, the current (incomplete) work-in-progress offers POCO support without attributes.


如果你是在谈论单个对象:

If you are talking about individual objects:

有相当简单的事情,你可以在.NET 3.5 防爆pression 在这里做的;建立动态防爆pression 基于反射,并调用 .Compile() MiscUtil 已经有这样的:

There are fairly simple things you can do here with Expression in .NET 3.5; build a dynamic Expression based on reflection, and call .Compile(). MiscUtil has this already:

DestType clone = PropertyCopy<DestType>.CopyFrom(original);

使用.NET 2.0 / 3.0(不含防爆pression ),你可能会考虑的HyperDescriptor 类似的目的。

With .NET 2.0/3.0 (without Expression) you might consider HyperDescriptor for similar purposes.

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

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