复制对象对象(Automapper?) [英] Copy object to object (with Automapper ?)

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

问题描述

我有一个类:

public class Person {
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

我有个人的两个实例(PERSON1和PERSON2)。我想复制到PERSON2 PERSON1的内容。我想打这个副本在一个指令和不可以物业属性:

person1.LastName = person2.LastName;

在医生,我看到一个对象复制到另一个对象,但类型不同。我怎样才能复制时的类型是一样的吗?对象

In the doc, I see copy an object to another object but the type is different. How can I copy the object when the type is the same ?

推荐答案

据我了解的问题,OP不想要克隆的 PERSON2 的成的新实例的的,但要求如何的的 PERSON2 的内容复制到一个现有实例 PERSON1 的)的的。还有的是,这是否对你AutoMapper的Mapper.Map方法的重载:

As I understand the question, OP does not want to clone person2 into a new instance of Person, but is asking for how to copy the contents of person2 into an already existing instance (person1) of Person. There is an overload of AutoMapper's Mapper.Map method that does this for you:

Mapper.CreateMap<Person, Person>();
Mapper.Map<Person, Person>(person2, person1);
//This copies member content from person2 into the _existing_ person1 instance.

注1: @ alexl的回答创建的的的的新的的实例。如果你有到实例 PERSON1 的其他引用指向,如果重定向的 PERSON1 的这些不会得到的(presumably)所需的数据更新变量为新实例。

Note 1: @alexl's answer creates a new instance of Person. If you have other references to the instance that person1 points to, these will not get the (presumably) desired data update if you redirect the person1 variable to a new instance.

注意2:您需要注意的是,(递归)复印深度取决于什么映射AutoMapper知道在映射的时刻结果!
如果人员中的一员的类是说阶级的的,你还做了 Mapper.CreateMap&LT;脑,脑,GT;(); 复制数据之前 Mapper.Map&LT;人,人&GT;(PERSON2,PERSON1); 通话,然后的 PERSON1 的将保持其目前的的实例,但这个的的将获得会员的 PERSON2 的的的的实例的值。那是你有一个深拷贝。结果
但是,如果AutoMapper没有复制前有脑脑图,然后的 PERSON1 的的的的成员将引用相同的的实例作为一的 PERSON2 的引用。这是你会得到一个浅拷贝。结果
这递归地适用于所有成员,因此,你最好确保AutoMapper有要深副本成员类映射,并且没有要浅副本成员类的映射。

Note 2: You need to be aware of that the (recursive) copying depth depends on what mappings AutoMapper knows about at the moment of mapping!
If a member of the Person class is of say the class Brain and you additionally have done Mapper.CreateMap<Brain, Brain>(); before the copy data Mapper.Map<Person, Person>(person2, person1); call, then person1 will keep its current Brain instance but this Brain will receive the member values of person2's Brain instance. That is you have a deep copy.
But if AutoMapper does not have a Brain-Brain mapping before copying, then person1's Brain member will reference the same Brain instance as the one person2 references. That is you will get a shallow copy.
This applies recursively to all members, so you better make sure AutoMapper has mappings for member classes that you want to deep copy, and doesn't have mappings for member classes that you want to shallow copy.

使用AutoMapper另一种方法是使用使用反射的方法。 (请注意,在链接code做了的的副本!)

An alternative to using AutoMapper would be to use an approach using reflection. (Note that the code in the link does a shallow copy!)

<子>填补现有对象,而不是AutoMapper创建目标对象本身支持是的加入AutoMapper 0.2版

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

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