Is AutoMapper able to auto resolve types base on existing maps

Posted by Chi Chan on Stack Overflow See other posts from Stack Overflow or by Chi Chan
Published on 2010-05-05T16:39:09Z Indexed on 2010/05/05 19:28 UTC
Read the original article Hit count: 278

Filed under:

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?

© Stack Overflow or respective owner

Related posts about automapper