Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I understand the question, OP does not want to clone <em>person2</em> into a new instance of <em>Person</em>, but is asking for how to copy the contents of <em>person2</em> into an <strong><em>already existing instance</em></strong> (<em>person1</em>) of <em>Person</em>. There is an overload of AutoMapper's Mapper.Map method that does this for you:</p> <pre><code>Mapper.CreateMap&lt;Person, Person&gt;(); Mapper.Map&lt;Person, Person&gt;(person2, person1); //This copies member content from person2 into the _existing_ person1 instance. </code></pre> <p><strong>Note 1:</strong> @alexl's answer creates a <em>new</em> instance of <em>Person</em>. If you have other references to the instance that <em>person1</em> points to, these will not get the (presumably) desired data update if you redirect the <em>person1</em> variable to a new instance.</p> <p><strong>Note 2:</strong> You need to be aware of that the (recursive) copying depth depends on what mappings AutoMapper knows about at the moment of mapping!<br> If a member of the <em>Person</em> class is of say the class <em>Brain</em> and you additionally have done <code>Mapper.CreateMap&lt;Brain, Brain&gt;();</code> before the copy data <code>Mapper.Map&lt;Person, Person&gt;(person2, person1);</code> call, then <em>person1</em> will keep its current <em>Brain</em> instance but this <em>Brain</em> will receive the member values of <em>person2</em>'s <em>Brain</em> instance. That is you have a <strong>deep copy</strong>.<br> But if AutoMapper does not have a Brain-Brain mapping before copying, then <em>person1</em>'s <em>Brain</em> member will reference the same <em>Brain</em> instance as the one <em>person2</em> references. That is you will get a <strong>shallow copy</strong>.<br> 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.</p> <p>An alternative to using AutoMapper would be to use <a href="http://www.west-wind.com/weblog/posts/2009/Aug/04/Simplistic-Object-Copying-in-NET">an approach using reflection</a>. (Note that the code in the link does a <em>shallow</em> copy!)</p> <p><sub>"Support for filling an existing object, instead of AutoMapper creating the destination object itself" was <a href="http://lostechies.com/jimmybogard/2009/02/11/automapper-0-2-released/">added in AutoMapper version 0.2</a>.</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload