title: AutoMapper - map to derived objects depend on condition
body: <p>I want to map source class to derived (from abstract) destination classes depend on value of some property.</p>
  
  <p>I have the following source classes:</p>
  
  <pre><code>public partial class ApplicationDriver
  {
      public virtual ICollection&lt;ApplicationDriverEquipment&gt; Equipments { get; set; }
  
  }
  
  public partial class ApplicationDriverEquipment
  {
      public int Id { get; set; }
      [StringLength(256)]
      public string Make { get; set; }
      [StringLength(256)]
      public string Model { get; set; }
      [StringLength(256)]
      public string Year { get; set; }
      [StringLength(256)]
      public string VINNumber { get; set; }
      [StringLength(256)]
      public string PlateNumber { get; set; }
      [StringLength(256)]
      public string CurrentMileage { get; set; }
      [StringLength(256)]
      public string Length { get; set; }
      public string Type { get; set; }
  
      public int DriverId { get; set; }
      public virtual ApplicationDriver Driver { get; set; }
  }
  </code></pre>
  
  <p>I want to map to the following classes, depend on Type parameter:</p>
  
  <pre><code>public class ApplicationDriverDomain
  {
      public List&lt;ApplicationDriverEquipmentAbstractDomain&gt; Equipments { get; set; }
  
  }
  
  public abstract class ApplicationDriverEquipmentAbstractDomain
  {
      public int Id { get; set; }
      public string Make { get; set; }
      public string Model { get; set; }
      public string Year { get; set; }
      public string PlateNumber { get; set; }
      public string CurrentMileage { get; set; }
      public string Type { get; protected set; }
  }
  
  public class ApplicationDriverEquipmentTractorDomain : ApplicationDriverEquipmentAbstractDomain
  {
      public ApplicationDriverEquipmentTractorDomain()
      {
          Type = ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor;
      }
      public string VINNumber { get; set; }
  }
  
  public class ApplicationDriverEquipmentTrailerDomain : ApplicationDriverEquipmentAbstractDomain
  {
      public ApplicationDriverEquipmentTrailerDomain()
      {
          Type = ApplicationDriverEquipmentTypeStaticStringsDomain.Trailer;
      }
  
      public string Length { get; set; }
  }
  
  public class ApplicationDriverEquipmentStraightTruckDomain : ApplicationDriverEquipmentAbstractDomain
  {
      public ApplicationDriverEquipmentStraightTruckDomain()
      {
          Type = ApplicationDriverEquipmentTypeStaticStringsDomain.StraightTruck;
      }
  
      public string VINNumber { get; set; }
      public string Length { get; set; }
  }
  
  public class ApplicationDriverEquipmentCargoVanDomain : ApplicationDriverEquipmentAbstractDomain
  {
      public ApplicationDriverEquipmentCargoVanDomain()
      {
          Type = ApplicationDriverEquipmentTypeStaticStringsDomain.CargoVan;
      }
  
      public string VINNumber { get; set; }
      public string Length { get; set; }
  }
  </code></pre>
  
  <p>I try to do it:</p>
  
  <pre><code>    ApplicationDriverEquipmentAbstractDomain GetEquipment(Infrastructure.Asset.ApplicationDriverEquipment infrastructure)
      {
          ApplicationDriverEquipmentAbstractDomain result = null;
          var config = new MapperConfiguration(cfg =&gt; cfg.AddProfile&lt;AutoMapperApplicationModel&gt;());
          var mapper = config.CreateMapper();
  
          switch (infrastructure.Type)
          {
              case ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor:
                  result = mapper.Map&lt;ApplicationDriverEquipmentTractorDomain&gt;(infrastructure);
                  break;
  
              case ApplicationDriverEquipmentTypeStaticStringsDomain.Trailer:
                  result = mapper.Map&lt;ApplicationDriverEquipmentTrailerDomain&gt;(infrastructure);
                  break;
  
              case ApplicationDriverEquipmentTypeStaticStringsDomain.StraightTruck:
                  result = mapper.Map&lt;ApplicationDriverEquipmentStraightTruckDomain&gt;(infrastructure);
                  break;
  
              case ApplicationDriverEquipmentTypeStaticStringsDomain.CargoVan:
                  result = mapper.Map&lt;ApplicationDriverEquipmentCargoVanDomain&gt;(infrastructure);
                  break;
  
          }
  
          return result;
      }
  
          CreateMap&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTractorDomain&gt;();
          CreateMap&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTrailerDomain&gt;();
          CreateMap&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentStraightTruckDomain&gt;();
          CreateMap&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentCargoVanDomain&gt;();
  
          CreateMap&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentAbstractDomain&gt;()
              .Include&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTractorDomain&gt;()
              .Include&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTrailerDomain&gt;()
              .Include&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentStraightTruckDomain&gt;()
              .Include&lt;Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentCargoVanDomain&gt;()
              .ForMember(dest =&gt; dest.Type, opt =&gt; opt.ResolveUsing(GetEquipment))
              ;
  
          CreateMap&lt;Infrastructure.Asset.ApplicationDriver, ApplicationDriverDomain&gt;()
              .ForMember(dest =&gt; dest.Equipments, opt =&gt; opt.MapFrom(src =&gt; src.Equipments));
  </code></pre>
  
  <p>but I got an error:</p>
  
  <blockquote>
    <p>"Error mapping types.\r\n\r\nMapping types:\r\nApplicationDriver ->
    ApplicationDriverDomain\r\nInfrastructure.Asset.ApplicationDriver ->
    Domain.POCO.Application.ApplicationDriverDomain\r\n\r\nType Map
    configuration:\r\nApplicationDriver ->
    ApplicationDriverDomain\r\nInfrastructure.Asset.ApplicationDriver ->
    Domain.POCO.Application.ApplicationDriverDomain\r\n\r\nProperty:\r\nEquipments"</p>
  </blockquote>