Пример #1
0
        /// <summary>
        /// 为分页集合进行automapper
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static PagedList <TResult> MapToPaged <TResult>(this IPagedList self)
        {
            if (self == null)
            {
                throw new ArgumentNullException();
            }
            var result = (PagedList <TResult>)Mapper.DynamicMap(self, self.GetType(), typeof(PagedList <TResult>));

            //显示的为分页参数赋值
            result.PageIndex  = self.PageIndex;
            result.PageSize   = self.PageSize;
            result.TotalCount = self.TotalCount;
            result.TotalPages = self.TotalPages;
            return(result);
        }
Пример #2
0
        public static T MapTo <T>(this IPagedList tList) where T : class
        {
            var totalCount = tList.TotalCount;
            var pageIndex  = tList.PageIndex;
            var pageSize   = tList.PageSize;
            //var groupSize = tList.GroupSize;


            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, totalCount, pageIndex, pageSize) as T;

            return(o2);
        }