如何深层复制不同类型的C#.NET对象之间 [英] How to deep copy between objects of different types in C#.NET

查看:199
本文介绍了如何深层复制不同类型的C#.NET对象之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求按字段名称映射所有ObjectV1和ObjectV2之间的字段值和儿童的集合。 ObjectV2是在一个不同的namspace到ObjectV1。

I have a requirement to map all of the field values and child collections between ObjectV1 and ObjectV2 by field name. ObjectV2 is in a different namspace to ObjectV1.

模板ClassV1和ClassV2之间的传承已贴现为这2类需要独立发展。我已用两个反射(这是慢)和二进制序列(这也是慢)执行的共同属性的映射考虑

Inheritence between the template ClassV1 and ClassV2 has been discounted as these 2 classes need to evolve independently. I have considered using both reflection (which is slow) and binary serialisation (which is also slow) to perform the mapping of the common properties.

有没有preferred的方法呢?还有没有其他办法?

Is there a preferred approach? Are there any other alternatives?

推荐答案

作为替代使用反射每次,您可以创建使用Reflection.Emit的动态地创建副本方法辅助类 - 这意味着你只能得到表现击中启动。这可能会给你的灵活性和性能,你需要的组合。

As an alternative to using reflection every time, you could create a helper class which dynamically creates copy methods using Reflection.Emit - this would mean you only get the performance hit on startup. This may give you the combination of flexibility and performance that you need.

由于Reflection.Emit的相当沉闷,我建议检查出<一href="http://www.$c$cplex.com/reflectoraddins/Wiki/View.aspx?title=ReflectionEmitLanguage&referringTitle=Home"相对=nofollow>这个反射器插件,这是辉煌的建设这类code。

As Reflection.Emit is quite clunky, I would suggest checking out this Reflector addin, which is brilliant for building this sort of code.

这篇关于如何深层复制不同类型的C#.NET对象之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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