这是我的View Model类:
public class EntryViewModel { }
在我的代码中,根据某个参数,我试图将其映射到:
基类:
public class Entry { }
或三个子类:
public class VacationEntry : Entry { }
public class WorkEntry : Entry { }
public class SickLeaveEntry : Entry { }
以下是我创建的地图:
Mapper.CreateMap<Entry, EntryViewModel>();
Mapper.CreateMap<EntryViewModel, Entry>();
Mapper.CreateMap<EntryViewModel, VacationEntry >()
.IncludeBase<EntryViewModel, Entry>();
Mapper.CreateMap<EntryViewModel, WorkEntry >()
.IncludeBase<EntryViewModel, Entry>();
Mapper.CreateMap<EntryViewModel, SickLeaveEntry >()
.IncludeBase<EntryViewModel, Entry>();
Mapper.AssertConfigurationIsValid();
现在,当我将View Model映射到Sub Class时,一切正常 - Mapper返回了我期待的类:
var entry = new EntryViewModel();
Entry result = Mapper.Map(entry, entry.GetType(), typeof(VacationEntry)) as Entry // result is type of VacationEntry
但是在尝试将ViewModel映射到Base Class时,我的结果是Sub Classes的最后一个CreatedMap的类型 - 在本例中是SickLeaveEntry:
var entry = new EntryViewModel();
Entry result = Mapper.Map(entry, entry.GetType(), typeof(Entry)) as Entry // result is type of SickLeaveEntry, but I was expecting Entry
有没有办法从AutoMapper获取基类?