开发者

Is AutoMapper able to auto resolve types base on existing maps

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 : Object开发者_运维问答Base { }

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?


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

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

Mapper.CreateMap<SourceObject, DestinationObject>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜