模板IEnumerable <t>的C#模板方法。有可能吗?</t>

时间:2013-01-03 09:16:10

标签: c# templates c#-3.0

有人可以帮我解决这个问题吗?

我有一个基类:

public class BaseShowFilter {
    public int    TotalCount { get; set; }  
    public int    FromNo { get; set; }
    public int    ShowCount { get; set; }
    public string SortFieldName { get; set; }
    public bool   SortAsc { get; set; }
}

和来自这个基类的几个ChildClasses。然后我有一些其他类存储(例如)

IEnumerable<OtherClassXXX> = ....

我想使用BaseShowFilter中实现的相同方法对所有这些过滤器应用一些过滤器:

例如我需要

dstList = srcList.Skip(this.FromNo-1).Take(this.ShowCount);

所以我需要在BaseShowFilter中实现一个函数,它将在参数IEnumerable中接受并且还将返回IEnumerable

我怎么写呢?在纯C ++中,它将很简单,如1,2,3 ......但在这里我不知道它是如何完成的。结果可能是这样的:

public class BaseShowFilter {
    public int    TotalCount { get; set; }  
    public int    FromNo { get; set; }
    public int    ShowCount { get; set; }
    public string SortFieldName { get; set; }
    public bool   SortAsc { get; set; }

    public T FilterList<T>(T SrcList) where T :IEnumerable<> {
        return srcList.Skip(this.FromNo-1).Take(this.ShowCount);
    }
}

1 个答案:

答案 0 :(得分:1)

这是通常的方法:

public IEnumerable<T> FilterList<T>(IEnumerable<T> source)
{
    return source.Skip(this.FromNo - 1).Take(this.ShowCount);
}
相关问题