是否可以创建匿名类型泛型?

时间:2011-04-07 21:52:58

标签: c# asp.net anonymous-types

我已经构建了一个适用于特定数据类型的分页类, 但现在我需要使类型动态

这是我的代码

public class Pagination {
    public IQueryable<Character> Items { get; set; }

    public int PageSize { get; set; }

    public int TotalPages {
        get {
            if (this.Items.Count() % this.PageSize == 0)
                return this.Items.Count() / this.PageSize;
            else
                return (this.Items.Count() / this.PageSize) + 1;
        }
    }

    public Pagination(IQueryable<Character> items, int pageSize) {
        this.PageSize = pageSize;
        this.Items = items;
    }

    public IQueryable<Character> GetPage(int pageNumber) {
        pageNumber = pageNumber - 1;
        return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize);
    }
}

如您所见,此分页类仅适用于'Character',是否可以创建匿名数据类型并调用Skip和Take等通用方法?

8 个答案:

答案 0 :(得分:5)

是的,您可以将该类创建为通用类:

public class Pagination<T> {
    public IQueryable<T> Items { get; set; }

    public int PageSize { get; set; }

    public int TotalPages {
        get {
            if (this.Items.Count() % this.PageSize == 0)
                return this.Items.Count() / this.PageSize;
            else
                return (this.Items.Count() / this.PageSize) + 1;
        }
    }

    public Pagination(IQueryable<T> items, int pageSize) {
        this.PageSize = pageSize;
        this.Items = items;
    }

    public IQueryable<T> GetPage(int pageNumber) {
        pageNumber = pageNumber - 1;
        return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize);
    }
}

答案 1 :(得分:3)

您只需将泛型用于基类并继承,实现该类。

public class Pagination <T> where T : class

然后将T放在您目前拥有Character类的所有位置

答案 2 :(得分:1)

这应该很简单...... 试试这个......

public class Pagination<_type> 
{
    public IQueryable<_type> Items { get; set; }

    public int PageSize { get; set; }

    public int TotalPages {
        get {
            if (this.Items.Count() % this.PageSize == 0)
                return this.Items.Count() / this.PageSize;
            else
                return (this.Items.Count() / this.PageSize) + 1;
        }
    }

    public Pagination(IQueryable<_type> items, int pageSize) {
        this.PageSize = pageSize;
        this.Items = items;
    }

    public IQueryable<_type> GetPage(int pageNumber) {
        pageNumber = pageNumber - 1;
        return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize);
    }
}

你可能想在_type上添加一个约束,就像这样(使得_type必须是一个类):

public class Pagination< _type > where _type : class

答案 3 :(得分:1)

这个通用类应该可以胜任。

public class Pagination<T>
{
    public IQueryable<T> Items { get; set; }

    public int PageSize { get; set; }

    public int TotalPages
    {
        get
        {
            if (this.Items.Count() % this.PageSize == 0)
                return this.Items.Count() / this.PageSize;
            else
                return (this.Items.Count() / this.PageSize) + 1;
        }
    }

    public Pagination(IQueryable<T> items, int pageSize)
    {
        this.PageSize = pageSize;
        this.Items = items;
    }

    public IQueryable<T> GetPage(int pageNumber)
    {
        pageNumber = pageNumber - 1;
        return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize);
    }
}

答案 4 :(得分:1)

是的,这是可能的。我会这样做:

  1. 隐藏实现,只显示一个接口:

    public interface IPagination<T>
    {
        int PageSize { get; set; }
        int TotalPages { get; }
        IQueryable<T> GetPage(int page);
    }
    
  2. 使Pagination类具有通用性,只有内部实现(外部只能看到界面)

    public class Pagination<T> : IPagination<T> {
        public IQueryable<T> Items { get; set; }
    
        public int PageSize { get; set; }
    
        public int TotalPages {
            get {
                        if (this.Items.Count() % this.PageSize == 0)
                        return this.Items.Count() / this.PageSize;
                    else
                        return (this.Items.Count() / this.PageSize) + 1;
                }
            }
    
            internal Pagination(IQueryable<T> items, int pageSize) {
                this.PageSize = pageSize;
                this.Items = items;
            }
    
            public IQueryable<T> GetPage(int pageNumber) {
                pageNumber = pageNumber - 1;
                return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize);
            }
    }
    
  3. 提供转换的扩展方法:

     public static IPagination<T> AsPagination (this IQueryable<T> source, int pageSize)
     {
         if(source == null)
             throw new ArgumentNullException("source");
         return new Pagination<T>(source, pageSize);
     }
    

答案 5 :(得分:0)

当然可以,但它通常只在方法体内有用。例如

var array = new[] {
  new { Name = "Jared", Age = 30 },
  new { Name = "Bob", Age = 21 }
};
var query = array.Skip(1).Take(1);

这在方法之间传递会变得有问题,因为无法说明匿名类型的名称,因此无需为T中的IEnumerable<T>添加任何内容。这可能与通用技巧有关,但它似乎不适合这种情况所需的

答案 6 :(得分:0)

这对你有用吗?

public class Pagination<T> {
    public IQueryable<T> Items { get; set; }

    public int PageSize { get; set; }

    public int TotalPages {
        get {
            if (this.Items.Count() % this.PageSize == 0)
                return this.Items.Count() / this.PageSize;
            else
                return (this.Items.Count() / this.PageSize) + 1;
        }
    }

    public Pagination(IQueryable<T> items, int pageSize) {
        this.PageSize = pageSize;
        this.Items = items;
    }

    public IQueryable<T> GetPage(int pageNumber) {
        pageNumber = pageNumber - 1;
        return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize);
    }
}

我不确定我是否看到与匿名类型的相关性?为什么需要使用此类型才能使用Character以外的类型?

答案 7 :(得分:0)

你试过这个吗?

public class Pagination<T> where T : class
{
    public IQueryable<T> Items { get; set; }

    public int PageSize { get; set; }

    public int TotalPages {
        get {
            if (this.Items.Count() % this.PageSize == 0)
                return this.Items.Count() / this.PageSize;
            else
                return (this.Items.Count() / this.PageSize) + 1;
        }
    }

    public Pagination(IQueryable<T> items, int pageSize) {
        this.PageSize = pageSize;
        this.Items = items;
    }

    public IQueryable<T> GetPage(int pageNumber) {
        pageNumber = pageNumber - 1;
        return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize);
    }
}
相关问题