AutoMapper帮助类

     /// <summary>
     /// AutoMapper帮助类
     /// </summary>
     public static class AutoMapperHelper
     {
         /// <summary>
         ///  类型映射
         /// </summary>
         public static T MapTo<T>(this object obj)
         {
             if (obj == null) return default(T);

             var config = new MapperConfiguration(cfg => cfg.CreateMap(obj.GetType(), typeof(T)));
             var mapper = config.CreateMapper();
             return mapper.Map<T>(obj);
         }

         /// <summary>
         /// 集合列表类型映射
         /// </summary>
         public static List<TDestination> MapToList<TDestination>(this IEnumerable<TDestination> source)
         {
             Type sourceType = source.GetType().GetGenericArguments()[];  //获取枚举的成员类型
             var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, typeof(TDestination)));
             var mapper = config.CreateMapper();

             return mapper.Map<List<TDestination>>(source);
         }

         /// <summary>
         /// 集合列表类型映射
         /// </summary>
         public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
         {
             var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
             var mapper = config.CreateMapper();

             return mapper.Map<List<TDestination>>(source);
         }

         /// <summary>
         /// 类型映射
         /// </summary>
         public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
             where TSource : class
             where TDestination : class
         {
             if (source == null) return destination;

             var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
             var mapper = config.CreateMapper();
             return mapper.Map<TDestination>(source);
         }
     }

AutoMapFromAttribute

 public class AutoMapFromAttribute : AutoMapAttributeBase
     {
         public MemberList MemberList { get; set; }

         public AutoMapFromAttribute(params Type[] targetTypes)
             : base(targetTypes)
         {
         }

         public AutoMapFromAttribute(MemberList memberList, params Type[] targetTypes)
             : this(targetTypes)
         {
             this.MemberList = memberList;
         }

         public override void CreateMap(IMapperConfigurationExpression configuration, Type type)
         {
             )
                 return;
             foreach (Type targetType in this.TargetTypes)
                 configuration.CreateMap(targetType, type, MemberList.Destination);
         }
         public static bool IsNullOrEmpty<T>(ICollection<T> source)
         {
             if (source != null)
                 ;
             return true;
         }

     }

AutoMapAttributeBase

  public abstract class AutoMapAttributeBase : Attribute
     {
         public Type[] TargetTypes { get; private set; }

         protected AutoMapAttributeBase(params Type[] targetTypes)
         {
             this.TargetTypes = targetTypes;
         }
         public abstract void CreateMap(IMapperConfigurationExpression configuration, Type type);
     }
05-11 15:56