automapper5 配置方法

automappper 5.0 更换了配置TSource 和TDestination 的方法。

详细查看:https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

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

            Mapper.Initialize(c => c.CreateMap(source.GetType(), typeof(TDestination)));
            var config = new MapperConfiguration(c => c.CreateMap(source.GetType(), typeof(TDestination)));
            var mapper = config.CreateMapper();
            return Mapper.Map<TDestination>(source);

            //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.Initialize(c => c.CreateMap(typeof(TSource), typeof(TDestination)));
            var config = new MapperConfiguration(c => c.CreateMap(typeof(TSource), typeof(TDestination)));
            var mapper = config.CreateMapper();
            return Mapper.Map<TSource, TDestination>(source, destination);

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

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

            Mapper.Initialize(c => c.CreateMap(typeof(TSource), typeof(TDestination)));
            var config = new MapperConfiguration(c => c.CreateMap(typeof(TSource), typeof(TDestination)));
            var mapper = config.CreateMapper();
            return (List<TDestination>)Mapper.Map(source, source.GetType(), typeof(List<TDestination>));

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