Is AutoMapper able to auto resolve types base on existing maps
- by Chi Chan
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?