这是我需要在左侧映射的两个类:
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));