我有一堂课:
internal class Paginated<T> where T : class
{
public List<T> data { get; set; }
public int total { get; set; }
public int page { get; set; }
public int per_page { get; set; }
public Paging paging { get; set; }
}
在另一个类中,我有方法:
public T RequestAllPaginated<T>() where T : Paginated<AnyType> //AnyType???
{
var item = Request<T>();
//Get all data from paging property
if(item != null)
{
var i = item as Paginated<AnyType>; //AnyType???
var data = i.data;
}
return item;
}
T始终是
Paginated<AnyType>
类型。问题是如何确定RequestAllPaginated
方法的声明并在以后进行转换? 最佳答案
试试这个解决方案
public T RequestAllPaginated<T, TK>() where T : Paginated<TK> where TK : class
{
var item = Request<T>();
//Get all data from paging property
if(item != null)
{
var i = item as Paginated<TK>;
var data = i.data;
}
return item;
}
替代解决方案:
public Paginated<T> RequestAllPaginated<T>() where T : class
{
var item = Request<Paginated<T>>();
//Get all data from paging property
if(item != null)
{
var i = item as Paginated<T>;
var data = i.data;
}
return item;
}