zoukankan      html  css  js  c++  java
  • AutoMapper简单用法

    首先在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);
    

      

  • 相关阅读:
    WAMP Apache 2.5 配置虚拟主机
    DOM对象
    BOM对象
    JS内置对象
    CSS定位
    CSS浮动和清除
    浏览器兼容性
    垂直居中
    水平居中总结
    长度值
  • 原文地址:https://www.cnblogs.com/zhtbk/p/6179302.html
Copyright © 2011-2022 走看看