左側に、マップする必要がある 2 つのクラスがあります。
class HumanSrc {
public int IQ;
public AnimalSrc Animal;
}
class AnimalSrc {
public int Weight;
}
右側は同じオブジェクトですが、継承を使用して構成されています。
class HumanDst : AnimalDst {
public int IQ;
}
class AnimalDst {
public int Weight;
}
したがって、必要なマッピングは次のとおりです。
humanSrc.IQ -> humanDst.IQ
humanSrc.Animal.Weight -> humanDst.Weight;
このマッピングは明示的に簡単に行うことができますが、すべて Animal から派生したいくつかのクラスがあり、Animal クラスは大きいため、Animal を一度マッピングしてから、すべての派生クラス マッピングにそれを含めることをお勧めします。
.Include<> メソッドを見ましたが、このシナリオをサポートしているとは思いません。
これが私が探しているものの本質です(疑似コード):
// define animal mapping
var animalMap = Mapper.CreateMap<AnimalSrc, AnimalDst>().ForMember(dst=>dst.Weight, opt=>opt.MapFrom(src=>src.Weight);
// define human mapping
var humanMap = Mapper.CreateMap<HumanSrc, HumanDst>();
humanMap.ForMember(dst=>dst.IQ, opt=>opt.MapFrom(src=>src.IQ));
// this is what I want. Basically I want to say:
// "in addition to that, map this child property on the dst object as well"
humanMap.ForMember(dst=>dst, opt=>opt.MapFrom(src=>src.Entity));