Copy object to object (using Automapper?)

I have a class:

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

I have two instances of Person (person1 and person2). I would like to copy the contents of person2 to person1. I would like to make this copy in one instruction and not in property:

 person1.LastName = person2.LastName; 

In the document, I see a copy of the object to another object, but the type is different. How to copy an object when the type is the same?

+58
c # automapper
Apr 19 '11 at 8:24
source share
4 answers

As I understand it, OP does not want to clone person2 into a new instance of Person, but asks how to copy the contents of person2 to an existing instance (person1) of Person. There is an overload of the AutoMapper 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. 

Note 1: @alexl answer creates a new instance of Person. If you have other references to the instance pointed to by person1, they will not receive the (supposedly) desired update of the data if you redirect the person1 variable to a new instance.

Note 2:. You should know that the (recursive) copy depth depends on which AutoMapper mappings know at the time of the mapping!
If a member of the Person class talks about the Brain class, and you additionally made Mapper.CreateMap<Brain, Brain>(); before calling the copy Mapper.Map<Person, Person>(person2, person1); , then person1 will retain its current instance of Brain, but this Brain will get the values ​​of the member person2. Instance of Brain, You have a deep copy .
But if AutoMapper does not have a Brain-Brain mapping before copying, the person1 member Brain will refer to the same Brain instance as the one person2 link. That is, you will receive a shallow copy .
This applies recursively to all members, so it’s best to make sure that AutoMapper has mappings for the member classes that you want to perform a deep copy and does not have mappings for the member classes that you want to place in a shallow copy.

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

"Support for adding an existing object instead of AutoMapper creating the target itself" was added in AutoMapper version 0.2 .

+67
Feb 28 '13 at 12:10
source share

So how did you ask With Automapper? May I suggest you not use AutoMapper?

Instead, use MemberwiseClone() in the Clone method, for example

 public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Person Clone() { return (Person) MemberwiseClone(); } } 

UPDATE

It is important to note that this does not achieve the initial desire of person1 copy person1 to person2

However (as @Jimmy Bogard points out) using MemberwiseClone() preferable if you just need to make a copy (clone) of the object.

For example, if you do this:

 //I need a copy of person1 please! I'll make a new person object //and automapper everything into it! var person2 = new Person2(); Mapper.Map<Person, Person>(person1, person2) 

then really you should / could use

 //oh wait, i can just use this! var person2 = person1.Clone() 
+25
Apr 20 2018-11-11T00:
source share
 Mapper.CreateMap<Person, Person>(); // Perform mapping var person1 = Mapper.Map<Person, Person>(person2); 

Hope this helps.

+17
Apr 19 '11 at 8:32
source share

Why do you want to use Automapper for this? A simple clone would do the job for you.

Read more here: Deep Cloning Objects

+2
Apr 19 2018-11-11T00:
source share



All Articles