首先在NuGet添加AutoMapper

 /// <summary>
    /// AutoMapper帮助类
    /// </summary>
    public static class AutoMapperHelper
    {
        /// <summary>
        ///  单个对象映射
        /// </summary>
        public static T MapTo<T>(this object obj)
        {
            if (obj == null) return default(T);
             Mapper.Initialize(x=>Mapper.CreateMap(obj.GetType(), typeof(T)));
            return Mapper.Map<T>(obj);
        }

        /// <summary>
        /// 集合列表类型映射
        /// </summary>
        public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
        {
            Mapper.Initialize(x => Mapper.CreateMap<TSource, TDestination>());
            return Mapper.Map<List<TDestination>>(source);
        }
    }

  字段对应问题

 Mapper.Initialize(x => Mapper.CreateMap<Temp, TempVo>()
                .ForMember(dest => dest.age1,
                    opts => opts.MapFrom(src => src.age)));

var testVo1 = Mapper.Map<Temp, TempVo>(test);

  

相关文章:

  • 2021-08-17
  • 2021-08-09
  • 2022-12-23
  • 2021-11-01
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案