如何通过BaseType获取Mapper
How to Get Mapper by BaseType
我想通过使用基本类型获得注释映射器(AutoMapper.Mapper.FindTypeMapFor)。
是否可以像用法中那样获取
AutoMapper.Mapper.CreateMap<IEntity, ListItemModelBase>()
.Include<Book, BookListItemModel>();
AutoMapper.Mapper.CreateMap<Book, BookListItemModel>()
.ForMember( a => a.Author, (a) => a.MapFrom( entity => entity.Author.Name))
.ForMember(a => a.Genre, (a) => a.MapFrom( entity => entity.Genre.Name));
AutoMapper.Mapper.CreateMap<BookListItemModel, Book>();
AutoMapper.Mapper.CreateMap<BookFormModel, Book>()
.ForMember(a => a.Id, (model) => model.PreCondition((context) => context.Id > 0))
.ForMember( a => a.UpdateDate, (model) => model.PreCondition((context) => context.Id > 0))
.ForMember( a => a.InsertDate, (model) => model.PreCondition((context) => context.Id == 0));
AutoMapper.Mapper.CreateMap<Book, BookFormModel>();
var mapper = AutoMapper.Mapper.FindTypeMapFor<IEntity, ListItemModelBase>();
//var mapper = AutoMapper.Mapper.FindTypeMapFor<Book, BookListItemModel>();
mapper.Dump();
mapper.GetPropertyMaps().Dump();
我想到的解决方案
var mapper = AutoMapper.Mapper
.GetAllTypeMaps()
.Where(a => a.SourceType == typeof(Book) && typeof(ListItemModelBase).IsAssignableFrom(a.DestinationType) && !a.DestinationType.IsAbstract)
.FirstOrDefault()
我想通过使用基本类型获得注释映射器(AutoMapper.Mapper.FindTypeMapFor)。
是否可以像用法中那样获取
AutoMapper.Mapper.CreateMap<IEntity, ListItemModelBase>()
.Include<Book, BookListItemModel>();
AutoMapper.Mapper.CreateMap<Book, BookListItemModel>()
.ForMember( a => a.Author, (a) => a.MapFrom( entity => entity.Author.Name))
.ForMember(a => a.Genre, (a) => a.MapFrom( entity => entity.Genre.Name));
AutoMapper.Mapper.CreateMap<BookListItemModel, Book>();
AutoMapper.Mapper.CreateMap<BookFormModel, Book>()
.ForMember(a => a.Id, (model) => model.PreCondition((context) => context.Id > 0))
.ForMember( a => a.UpdateDate, (model) => model.PreCondition((context) => context.Id > 0))
.ForMember( a => a.InsertDate, (model) => model.PreCondition((context) => context.Id == 0));
AutoMapper.Mapper.CreateMap<Book, BookFormModel>();
var mapper = AutoMapper.Mapper.FindTypeMapFor<IEntity, ListItemModelBase>();
//var mapper = AutoMapper.Mapper.FindTypeMapFor<Book, BookListItemModel>();
mapper.Dump();
mapper.GetPropertyMaps().Dump();
我想到的解决方案
var mapper = AutoMapper.Mapper
.GetAllTypeMaps()
.Where(a => a.SourceType == typeof(Book) && typeof(ListItemModelBase).IsAssignableFrom(a.DestinationType) && !a.DestinationType.IsAbstract)
.FirstOrDefault()