C#将泛型委托传递给构造函数

时间:2016-06-28 21:42:58

标签: c# generics constructor delegates

我是C#的新手,我试图找到一种将泛型委托传递给构造函数的方法。 调用show endpoint时,如果存在分页,则端点应返回Page实例。创建Page实例时,委托方法应作为构造函数参数之一传递。 ShowsEndpoints将托管许多端点,其中许多端点使用分页。我想只使用一个类来处理所有端点。 public partial class ShowsEndpoints:Endpoint {     public Func< Task< IEnumerable< object>>> FetchPopularShowsAsyncDelegate;     public Func< Task< IEnumerable< object>>> FetchTrendingShowsAsyncDelegate;     public Func< Task< IEnumerable< object>>> FetchMostWatchedShowsAsyncDelegate;     public async Task< IEnumerable< object>> FetchPopularShowsAsync(int page = 1,int limit = 20)     {         return await SendAsync(new PopularShowsRequest(page,limit));     }     public async Task< IEnumerable< object>> FetchTrendingShowsAsync(int page = 1,int limit = 20)     {         return await SendAsync(new TrendingShowsRequest(page,limit));     }     public async Task< IEnumerable< object>> FetchMostWatchedShowsAsync(int page = 1,int limit = 20)     {         return await SendAsync(new MostWatchedShowsRequest(page,limit));     } } 页面类,如果有下一页,将调用委托以获取下一页。 公共类Page< TItem,TDelegate> {     私人TDelegate _delegateCommand;     public int CurrentPage {get; }     public int TotalPages {get; }     public int PageItemsCount {get; }     public int TotalItemsCount {get; }     public IEnumerable< TItem>物品{get; }     public Page(int page,int totalPages,int itemsCount,int totalItemsCount,IEnumerable< TItem> items,TDelegate delegateCommand)     {         CurrentPage = page;         TotalPages = totalPages;         PageItemsCount = itemsCount;         TotalItemsCount = totalItemsCount;         Items = items;         _delegateCommand = delegateCommand;     }     public bool HasNext()     {         return CurrentPage< = TotalPages;     }     public bool HasPrevious()     {         return CurrentPage> = 1;     }     公共页面< TItem,TDelegate>下一个()     {         //调用_delegateCommand来获取下一个页面项     }     公共页面< TItem,TDelegate>以前()     {         //调用_delegateCommand来获取上一页的项目     } } 可能吗 ? 谢谢。 编辑 我已经更新了我的类代码,以便更好地理解我的问题。我有3个与其委托关联的端点,我希望Page类使用具有相同代码的这3个委托中的任何一个,以避免重复。所以_delegateCommand可以是FetchPopularShowsAsyncDelegate,FetchTrendingShowsAsyncDelegate或FetchMostWatchedShowsAsyncDelegate。

1 个答案:

答案 0 :(得分:0)

我不确定我是否有_delegateCommand权利的目的 - 我想它应该是返回IEnumerable<TItem>的函数。如果是这样,那么你可以像那样声明它,然后你不必引入另一个通用参数:

public class Page<TItem>
{
    private Func<IEnumerable<TItem>>_delegateCommand;

    public int CurrentPage { get; }
    public int TotalPages { get; }
    public int PageItemsCount { get; }
    public int TotalItemsCount { get; }
    public IEnumerable<TItem> Items { get; }

    public Page(int page, int totalPages, int itemsCount,
                int totalItemsCount, IEnumerable<TItem> items,
                Func<IEnumerable<TItem>> delegateCommand)
    {
        CurrentPage = page;
        TotalPages = totalPages;
        PageItemsCount = itemsCount;
        TotalItemsCount = totalItemsCount;
        Items = items;
        _delegateCommand = delegateCommand;
    }

    public bool HasNext()
    {
        return CurrentPage <= TotalPages;
    }

    public bool HasPrevious()
    {
        return CurrentPage >= 1;
    }
}