I am not sure if this is a Covariance and Contravariance issue but I cannot get this working. Here is the code:
public interface IDto { }
public class Pagin
Yes this is a variance issue. You need to create an interface (only interfaces and delegates can be co/contravariant) IPaginatedDto
where the Dtos
cannot have a setter (otherwise you cannot use out
):
public interface IPaginatedDto where TDto : IDto
{
int PageIndex { get; set; }
int PageSize { get; set; }
int TotalCount { get; set; }
int TotalPageCount { get; set; }
bool HasNextPage { get; set; }
bool HasPreviousPage { get; set; }
IEnumerable Dtos { get; }
}
And your PaginatedDto
will implement this interface:
public class PaginatedDto : IPaginatedDto where TDto : IDto
{
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPageCount { get; set; }
public bool HasNextPage { get; set; }
public bool HasPreviousPage { get; set; }
public IEnumerable Dtos { get; set; }
}
And use the interface in your method:
private static void ProcessDto(IPaginatedDto paginatedDto)
{
//Do work...
}