tags:

views:

82

answers:

1

I have the following code:

[SetUp]
public void SetMeUp()
{
     Mapper.CreateMap<SourceObject, DestinationObject>();
}

[Test]
public void Testing()
{
     var source = new SourceObject {Id = 123};
     var destination1 = Mapper.Map<SourceObject, DestinationObject>(source);
     var destination2 = Mapper.Map<ObjectBase, ObjectBase>(source);

     //Works
     Assert.That(destination1.Id == source.Id);

     //Fails, gives the same object back
     Assert.That(destination2 is DestinationObject);
}

public class ObjectBase
{
     public int Id { get; set; }
}

public class SourceObject : ObjectBase { }
public class DestinationObject : ObjectBase { }

So basically, I want AutoMapper to automatically resolve the destination type to "DestinationObject" based on the existing Maps set up in AutoMapper. Is there a way to achieve this?

+1  A: 

You could try the following mapping with the latest version (1.1):

Mapper.CreateMap<ObjectBase,ObjectBase>()
  .Include<SourceObject, DestinationObject>();

Mapper.CreateMap<SourceObject, DestinationObject>();
miensol
Thanks, exactly what I need. On top of that, 1.1 was release just yesterday, perfect timing!
Chi Chan

related questions