问题
public class Flight {
public CabinCollection Cabins { get; set; }
}
public class CabinCollection : List<Cabin>
{
public Cabin Lowest { set; get; }
}
source and dest class have the same members
1) Mapper.Initialize(cfg => {
cfg.CreateMap<Domain.Flight, Contract.Flight>();
cfg.CreateMap<Domain.Cabin, Contract.Cabin>();
});
List<Flight> res = Mapper.Map<List<Flight>>(flights);
It works but the member 'lowest' is null
2) Mapper.Initialize(cfg => {
cfg.CreateMap<Domain.Flight, Contract.Flight>();
cfg.CreateMap<Domain.Cabin, Contract.Cabin>();
cfg.CreateMap<Domain.CabinCollection,Contract.CabinColection>
.IncludeBase<List<Domain.Cabin>, List<Contract.Cabin>>()
});
It works and the member 'lowest' mapped, but the list is null
Is there a way make it right?
回答1:
Add this to your configuration
cfg.CreateMap<Domain.CabinCollection, Contract.CabinCollectionDest>();
Basically Automapper wouldn't know how to map CabinCollection
object
.Net fiddle link to show this
来源:https://stackoverflow.com/questions/44015032/automapper-mapping-inheritance-from-generic-list