ソース クラスと 2 つの宛先クラスがあるとします。
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; }
}
Automapper 3.* の場合、このコードは完全に機能しました。
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);
ただし、4.0.4 にアップグレードした後、このマッピングは例外をスローします。
System.InvalidCastException: Unable to cast object of type 'DestinationBase' to type 'DestinationDerived'
私が間違っていることはありますか、それとも AutoMapper のバグですか?
.net フィドルのコード:
- 3.3.1 バージョン: https://dotnetfiddle.net/6K9PtK
- 4.0.4 バージョン: https://dotnetfiddle.net/0mF8y8