国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Automapper擴展方法

2019-11-17 02:30:36
字體:
來源:轉載
供稿:網友

Automapper擴展方法

問題描述

系統中實現了一個自定義的PagedList

/// <summary>    /// Paged list interface    /// </summary>    public interface ipagedList    {        int PageIndex { get; }        int PageSize { get; }        int TotalCount { get; }        int TotalPages { get; }        bool HaspreviousPage { get; }        bool HasNextPage { get; }    }    /// <summary>    /// Paged list interface    /// </summary>    public interface IPagedList<T> : IList<T>, IPagedList    {    }/// <summary>    /// Paged list    /// </summary>    /// <typeparam name="T">T</typeparam>    [Serializable]    public class PagedList<T> : List<T>, IPagedList<T>     {        /// <summary>        /// Ctor        /// </summary>        /// <param name="source">source</param>        /// <param name="pageIndex">Page index</param>        /// <param name="pageSize">Page size</param>        public PagedList(IQueryable<T> source, int pageIndex, int pageSize)        {            int total = source.Count();            this.TotalCount = total;            this.TotalPages = total / pageSize;            if (total % pageSize > 0)                TotalPages++;            this.PageSize = pageSize;            this.PageIndex = pageIndex;            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());        }        /// <summary>        /// Ctor        /// </summary>        /// <param name="source">source</param>        /// <param name="pageIndex">Page index</param>        /// <param name="pageSize">Page size</param>        public PagedList(IList<T> source, int pageIndex, int pageSize)        {            TotalCount = source.Count();            TotalPages = TotalCount / pageSize;            if (TotalCount % pageSize > 0)                TotalPages++;            this.PageSize = pageSize;            this.PageIndex = pageIndex;            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());        }        /// <summary>        /// Ctor        /// </summary>        /// <param name="source">source</param>        /// <param name="pageIndex">Page index</param>        /// <param name="pageSize">Page size</param>        /// <param name="totalCount">Total count</param>        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)        {            TotalCount = totalCount;            TotalPages = TotalCount / pageSize;            if (TotalCount % pageSize > 0)                TotalPages++;            this.PageSize = pageSize;            this.PageIndex = pageIndex;            this.AddRange(source);        }        public int PageIndex { get; PRivate set; }        public int PageSize { get; private set; }        public int TotalCount { get; private set; }        public int TotalPages { get; private set; }        public bool HasPreviousPage        {            get { return (PageIndex > 0); }        }        public bool HasNextPage        {            get { return (PageIndex + 1 < TotalPages); }        }    }

轉換失敗,AutoMapper可不認識PagedList

 [TestMethod]        public void MapTest2()        {            Mapper.CreateMap<User, UserViewModel>();            var userList = new List<User>()            {                new User() {Name = "Name1", UserId = "UserId1"},                new User() {Name = "Name2", UserId = "UserId2"},                new User() {Name = "Name3", UserId = "UserId3"},                new User() {Name = "Name4", UserId = "UserId4"},                new User() {Name = "Name5", UserId = "UserId5"},                new User() {Name = "Name6", UserId = "UserId6"},                new User() {Name = "Name7", UserId = "UserId7"},            };            var pagedList = new PagedList<User>(userList, 0, 5);                        Mapper.Map<PagedList<User>, PagedList<UserViewModel>>(pagedList);//Exception                    }

解決方案

 /// <summary>    /// The collection extensions.    /// </summary>    public static class ObjectExtensions    {        private static readonly MethodInfo mapMethod;        private static readonly ConcurrentDictionary<Tuple<Type, Type>, Tuple<MethodInfo, Type>> methodsMapper = new ConcurrentDictionary<Tuple<Type, Type>, Tuple<MethodInfo, Type>>();        static ObjectExtensions()        {            mapMethod = (typeof(Mapper)).GetMethods().FirstOrDefault(_ => _.Name == "Map" && _.GetParameters().Length == 1);        }        public static T MapTo<T>(this IPagedList tList) where T : class        {            var totalCount = tList.TotalCount;            var pageIndex = tList.PageIndex;            var pageSize = tList.PageSize;            var t = methodsMapper.GetOrAdd(new Tuple<Type, Type>(tList.GetType(), typeof(T)), _ =>            {                var targetGenericArguments = typeof(T).GenericTypeArguments[0];                var targetGenericArgumentsIEnumerableType = typeof(IEnumerable<>).MakeGenericType(targetGenericArguments);                return new Tuple<MethodInfo, Type>(mapMethod.MakeGenericMethod(targetGenericArgumentsIEnumerableType),                    typeof(PagedList<>).MakeGenericType(targetGenericArguments));            });            var rtn2 = t.Item1.Invoke(null, new object[] { tList });            var o2 = Activator.CreateInstance(t.Item2, rtn2, pageIndex, pageSize, totalCount) as T;            return o2;        }        public static T MapTo<T>(this object o) where T : class        {            //way1            //var mapMethod = (typeof(Mapper)).GetMethods().FirstOrDefault(_ => _.Name == "Map" && _.GetParameters().Length == 1 && _.GetGenericArguments().Length == 2 );            //var m2 =  mapMethod.MakeGenericMethod(o.GetType(), typeof (T));            //return m2.Invoke(null, new[] { o }) as T;            //way2            return Mapper.Map<T>(o);        }        public static void MapTo<S,T>(this S o,T t) where T : class        {            Mapper.Map(o,t);        }    }

測試通過

[TestMethod]        public void MapTest2()        {            Mapper.CreateMap<User, UserViewModel>();            var userList = new List<User>()            {                new User() {Name = "Name1", UserId = "UserId1"},                new User() {Name = "Name2", UserId = "UserId2"},                new User() {Name = "Name3", UserId = "UserId3"},                new User() {Name = "Name4", UserId = "UserId4"},                new User() {Name = "Name5", UserId = "UserId5"},                new User() {Name = "Name6", UserId = "UserId6"},                new User() {Name = "Name7", UserId = "UserId7"},            };            var pagedList = new PagedList<User>(userList, 0, 5);            var vmPagedList = pagedList.MapTo<PagedList<UserViewModel>>();            Assert.IsTrue(vmPagedList.TotalPages == 2                 && vmPagedList.PageSize == 5                && vmPagedList.PageIndex == 0                );        }

總結

運行時動態獲取泛型參數并執行Mapper.Map<IEnumerable<TSource>, IEnumerable<TDestination>>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 稻城县| 辽阳市| 太和县| 惠州市| 衡阳市| 永春县| 北宁市| 涿州市| 玛多县| 筠连县| 胶南市| 贵州省| 星子县| 赤壁市| 凌源市| 北宁市| 城市| 偏关县| 浏阳市| 大悟县| 芜湖市| 莲花县| 中山市| 余干县| 长春市| 元谋县| 宁明县| 大余县| 霞浦县| 毕节市| 原阳县| 历史| 青龙| 扎兰屯市| 伊通| 阿尔山市| 临海市| 宣威市| 民权县| 繁峙县| 龙岩市|