我不确定这是否是协方差和逆变问题,但我无法使其正常工作。这是代码:
public interface IDto { }
public class PaginatedDto<TDto> 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<TDto> Dtos { get; set; }
}
public class PersonDto : IDto {
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
}
class Program {
static void Main(string[] args) {
var people = new List<PersonDto> {
new PersonDto { },
new PersonDto { },
new PersonDto { },
};
var paginatedPersonDto = new PaginatedDto<PersonDto>() {
Dtos = people
};
//ProcessDto doesn't accept this
ProcessDto(paginatedPersonDto);
}
private static void ProcessDto(PaginatedDto<IDto> paginatedDto) {
//Do work...
}
}
由于某种原因,我无法通过PaginatedDto<PersonDto>
方法。知道如何解决这个问题吗?PaginatedDto<IDto>
ProcessDto