AutoMapper简单用法
首先在NuGet添加AutoMapper
////// AutoMapper帮助类 /// public static class AutoMapperHelper { ////// 单个对象映射 /// public static T MapTo(this object obj) { if (obj == null) return default(T); Mapper.Initialize(x=>Mapper.CreateMap(obj.GetType(), typeof(T))); return Mapper.Map (obj); } /// /// 集合列表类型映射 /// public static ListMapToList (this IEnumerable source) { Mapper.Initialize(x => Mapper.CreateMap ()); return Mapper.Map >(source); } }
字段对应问题
Mapper.Initialize(x => Mapper.CreateMap() .ForMember(dest => dest.age1, opts => opts.MapFrom(src => src.age))); var testVo1 = Mapper.Map (test);