title: Use AutoMapper with custom conversion on a specific property
body: <p>Dear Internet community.</p>
  
  <p>I need to map elements from one object to another. The classes are identical (same class name, same properties), so I  figure we should give AutoMapper a try instead. It seems to work pretty well, but I've hit a snag: One class has an <code>object</code> type property that is used as sort of a 'wild-card' container.</p>
  
  <pre><code>public class MyPet
  {
      public int Id { get; set; }
      public object Pet{ get; set; }
  }
  
  public class Cat
  {
  
  }
  
  public class Dog
  {
  
  }
  </code></pre>
  
  <p>I intially figured that AutoMapper would be able to recognize the type of the instance and perform the appropriate mapping as instructed:</p>
  
  <pre><code>Mapper.CreateMap&lt;LocalService.MyPet, ExternalService.MyPet&gt;();
  Mapper.CreateMap&lt;LocalService.Cat, ExternalService.Cat&gt;();
  Mapper.CreateMap&lt;LocalService.Dog, ExternalService.Dog&gt;();
  
  
  var dtoWithCat0 = new LocalService.MyPet()
  {
      Id = 1,
      Item = new LocalService.Cat()
  };
  
  var dtoWithDog0 = new LocalService.MyPet()
  {
      Id = 2,
      Item = new LocalService.Dog()
  };
  
  var dtoWithCat1 = Mapper.Map&lt;ExternalService.MyPet&gt;(dtoWithCat0);
  
  var dtoWithDog1 = Mapper.Map&lt;ExternalService.MyPet&gt;(dtoWithDog0);
  
  Console.WriteLine("{0}: {1} - {2}", dtoWithCat1.GetType().FullName, dtoWithCat1.Id, dtoWithCat1.Item.GetType().FullName);
  
  Console.WriteLine("{0}: {1} - {2}", dtoWithCat1.GetType().FullName, dtoWithDog1.Id, dtoWithDog1.Item.GetType().FullName);
  </code></pre>
  
  <p>However, this is the output:</p>
  
  <pre><code>ExternalService.MyPet: 1 - LocalService.Cat
  ExternalService.MyPet: 2 - LocalService.Dog
  </code></pre>
  
  <p>As you can see, AutoMapper is happy to copy the object reference of the property <code>Item</code> instead of creating a new ExternalService instance.</p>
  
  <p>I'm looking for a way to instruct AutoMapper to resolve the <code>Item</code> property based on a custom function, something like this:</p>
  
  <pre><code>object ConvertPet(object source)
  {
      if (source is LocalService.Cat) return Mapper.Map&lt;ExternalService.Cat&gt;(source as LocalService.Cat);
      if (source is LocalService.Dog) return Mapper.Map&lt;ExternalService.Dog&gt;(source as LocalService.Dog);
  }
  </code></pre>
  
  <p>Any tips would be appreciated!</p>
  
  <p>-S</p>
  
  <p>PS. I notice that there exists a <code>ConvertUsing</code> method. Unfortunately, this appears to replace the "automatic" part of AutoMapper with an custom explicit mapping that needs to implement mapping of all properties on the DTO. I would like to be lazy and have AutoMapper make an exception just for that single property...</p>