AutoMapper 扩展

        /// <summary>
        /// AutoMapper 
        /// </summary>
        public static TDestination MapTo<TDestination>(this object source)
        {
            if (source == null)
                throw new ArgumentNullException();

            Mapper.CreateMap(source.GetType(), typeof(TDestination));
            return (TDestination)Mapper.Map<TDestination>(source);
        }

        /// <summary>
        /// AutoMapper 
        /// </summary>
        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
        {
            if (source == null)
                return destination;

            Mapper.CreateMap(typeof(TSource), typeof(TDestination));
            return Mapper.Map<TSource, TDestination>(source, destination);
        }

        /// <summary>
        /// AutoMapper 
        /// </summary>
        public static List<TDestination> MapTo<TDestination>(this IEnumerable source) where TDestination : IEnumerable
        {
            if (source == null)
                throw new ArgumentNullException();

            Mapper.CreateMap(source.GetType(), typeof(List<TDestination>));
            return (List<TDestination>)Mapper.Map(source, source.GetType(), typeof(List<TDestination>));
        }

        /// <summary>
        /// AutoMapper 
        /// </summary>
        public static IEnumerable MapTo<TSource, TDestination>(this IEnumerable source, IEnumerable destination)
            where TSource : IEnumerable
            where TDestination : IEnumerable
        {
            if (source == null)
                return destination;

            Mapper.CreateMap(source.GetType(), typeof(List<TDestination>));
            return Mapper.Map<IEnumerable, IEnumerable>(source, destination);
        }

已禁用评论。