首页 新闻 会员 周边 捐助

关于动态绑定泛型参数的疑问

0
[已解决问题] 解决于 2009-03-07 12:21
Code


这个类是用来数据分页后的结果的,total为记录总数,dataList为分页后的结果集,由于很多数据类都需要分页,都以这种格式返回,但是泛型T是预先不可知的,需要动态获取,有什么办法做到这一点呢,除了想到反射,头疼了一晚上没弄成?有没有简洁而有效地方案?

问题补充: private Type type; 这个是多余的,发错了 水平有限,总是提问,我的分都散完了,还请园友们见谅!
Luffy Huang的主页 Luffy Huang | 初学一级 | 园豆:25
提问于:2009-03-06 04:58
< >
分享
最佳答案
0

你这个类本身也应该是个泛型类才对哦

public class DataPage<T>

{

    public int Total { get; set; }

    public List<T> DataList { get; set; }

}

顺便给你个我一直用的分页类

[Serializable]
[DataContract]
public class PagedQuery<TItem> : MultipleQuery<TItem>
{
    #region Statics

    /// <summary>
    /// The default page size.
    /// </summary>
    public readonly int DefaultPageSize = 10;

    #endregion

    #region Properties

    private long m_PageIndex;

    /// <summary>
    /// Gets the one-based index of page of current <see cref="PagedQuery&lt;TItem&gt;"/>.
    /// </summary>
    [DataMember]
    public long PageIndex
    {
        get
        {
            return m_PageIndex;
        }
        private set
        {
            if (value <= 0)
            {
                m_PageIndex = 1;
            }
            else
            {
                m_PageIndex = value;
            }
        }
    }

    private int m_PageSize;

    /// <summary>
    /// Gets the count of items in a page of current <see cref="PagedQuery&lt;TItem&gt;"/>.
    /// </summary>
    [DataMember]
    public int PageSize
    {
        get
        {
            return m_PageSize;
        }
        private set
        {
            if (value < 0)
            {
                m_PageSize = DefaultPageSize;
            }
            else
            {
                m_PageSize = value;
            }
        }
    }

    /// <summary>
    /// Gets the total records of current <see cref="PagedQuery&lt;TItem&gt;"/>.
    /// </summary>
    [DataMember]
    public long TotalRecords
    {
        get;
        private set;
    }

    #endregion

    #region Computed Properties

    /// <summary>
    /// Gets the index of the first item in current page within all objects.
    /// </summary>
    public long StartIndex
    {
        get
        {
            return (checked(((PageIndex - 1) * PageSize) + 1));
        }
    }

    /// <summary>
    /// Gets the index of the last item in current page within all objects.
    /// </summary>
    public long EndIndex
    {
        get
        {
            return (checked(StartIndex + PageSize - 1));
        }
    }

    /// <summary>
    /// Gets the number of items that should be skipped before reach the first item in current page.
    /// </summary>
    public long SkipCount
    {
        get
        {
            return (checked((PageIndex - 1) * PageSize));
        }
    }

    /// <summary>
    /// Gets the number of pages.
    /// </summary>
    public int PageCount
    {
        get
        {
            if (TotalRecords <= 0)
            {
                return 0;
            }
            else if (TotalRecords % PageSize > 0)
            {
                return (checked((int)(TotalRecords / PageSize + 1)));
            }
            else
            {
                return (checked((int)(TotalRecords / PageSize)));
            }
        }
    }

    /// <summary>
    /// Determines whether current page has a previous page.
    /// </summary>
    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex <= 1);
        }
    }

    /// <summary>
    /// Determines whether current page has a next page.
    /// </summary>
    public bool HasNextPage
    {
        get
        {
            return (PageIndex >= PageCount);
        }
    }

    #endregion

    #region Constructors

    /// <summary>
    /// Creates an instance of <see cref="PagedQuery&lt;TItem&gt;"/> class.
    /// </summary>
    /// <param name="pageIndex">The one-based index of page.</param>
    /// <param name="pageSize">The count of items in a page.</param>
    public PagedQuery(long pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        PageSize = pageSize;
    }

    #endregion

    #region Methods

    /// <summary>
    /// Fills current <see cref="PagedQuery&lt;TItem&gt;"/> with result.
    /// </summary>
    /// <param name="result">The result to fill current <see cref="PagedQuery&lt;TItem&gt;"/></param>
    /// <param name="totalRecords">The total records of query.</param>
    public void Fill(IEnumerable<TItem> result, long totalRecords)
    {
        Fill(result);

        if (totalRecords < 0)
        {
            throw new ArgumentOutOfRangeException(
                "Never can a query return total records less than zero...", (Exception)null);
        }

        TotalRecords = totalRecords;
    }

    /// <summary>
    /// Gets the previous page (unfilled).
    /// </summary>
    /// <returns>The previous page or null if this is the first page.</returns>
    public PagedQuery<TItem> GetPreviousPage()
    {
        return HasPreviousPage ? new PagedQuery<TItem>(PageIndex - 1, PageSize) : (PagedQuery<TItem>)null;
    }

    /// <summary>
    /// Gets the next page (unfilled).
    /// </summary>
    /// <returns>The next page or null if this is the first page.</returns>
    public PagedQuery<TItem> GetNextPage()
    {
        return HasNextPage ? new PagedQuery<TItem>(PageIndex + 1, PageSize) : (PagedQuery<TItem>)null;
    }

    /// <summary>
    /// Gets the first page (unfilled).
    /// </summary>
    /// <returns>The first page.</returns>
    public PagedQuery<TItem> GetFirstPage()
    {
        return new PagedQuery<TItem>(1, PageSize);
    }

    /// <summary>
    /// Gets the last page (unfilled).
    /// </summary>
    /// <returns>The last page.</returns>
    public PagedQuery<TItem> GetLastPage()
    {
        return new PagedQuery<TItem>(PageCount, PageSize);
    }

    #endregion
}

其类MultipleQuery<T>没什么,就是有一个List<T>的Result属性,这个分页类控制了页数和每页显示的数量肯定不会是负数,同时提供了总页数,是否有前一页,是否有后一页,当前页第一个元素的索引,当前页最后一个元素的索引等等功能,配合写SQL语句方便不少

Gray Zhang | 专家六级 |园豆:17610 | 2009-03-06 08:42
其他回答(2)
0

一般就是用反射了,或者用接口也可以,如果用接口,必须能抽象出统一的接口。

eaglet | 园豆:17139 (专家六级) | 2009-03-06 10:11
0

这个类也需要是泛型类,Grace Zhang就是正解!

Jun1st | 园豆:240 (菜鸟二级) | 2009-03-06 17:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册