Suppose I have a source class and two destination classes, one more general and one more specific (which inherits from the more general one):
public class Source
{
public int A { get; set; }
public string Str { get; set; }
}
public class DestinationBase
{
public int A { get; set; }
}
public class DestinationDerived : DestinationBase
{
public string Str { get; set; }
}
For Automapper 3.*, this code worked perfectly:
Mapper.Initialize(cfg => {
cfg.CreateMap<Source, DestinationBase>();
cfg.CreateMap<Source, DestinationDerived>()
.IncludeBase<Source, DestinationBase>();
});
var src = new Source() { A = 1, Str = "foo" };
var dest = new DestinationBase();
Mapper.Map(src, dest);
However, after upgrading to 4.0.4, this mapping throws exception:
System.InvalidCastException: Unable to cast object of type 'DestinationBase' to type 'DestinationDerived'
Is there anything I'm doing wrong or is this a bug in AutoMapper?
The code in .net fiddle:
- 3.3.1 version: https://dotnetfiddle.net/6K9PtK
- 4.0.4 version: https://dotnetfiddle.net/0mF8y8