Automapper with inherited list

2020-05-06 16:27发布

FYI: it's different from this question Automapper and inheritance from Collection or List

Here is my inherited list:

public class MyPagedList<T> : List<T>
{
    public int CurrentPage { get; set; }
    public int TotalPages { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }

    public MyPagedList()
    {
    }

    public MyPagedList(List<T> items, int count, int pageNumber, int pageSize)
    {
        TotalCount = count;
        PageSize = pageSize;
        CurrentPage = pageNumber;
        TotalPages = (int)Math.Ceiling(count / (double)pageSize);
        AddRange(items);
    }
}

Here are my objects

public class A
{
    public string Ant { get; set; }
    public string Apple { get; set; }
}

public class B
{
    public string Ant { get; set; }
    public string Ansi { get; set; }
}

Finally I am calling these like this

        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<A, B>()
               .ForMember(m => m.Ansi, opt => opt.MapFrom(x => x.Apple));

        });

        var _mapper = config.CreateMapper();
        var fixture = new Fixture();
        fixture.RepeatCount = 5;
        var product = fixture.Create<List<A>>();
        var Alist = new MyPagedList<A>(product, 5, 1, 1);

        var Blist = _mapper.Map<MyPagedList<A>, MyPagedList<B>>(Alist);

The problem here is my BList.TotalPages is always 0. Somehow the content of the list is being mapped but not the property of MyPagedList like CurrentPage, PageSize and so forth.How can I map the MyPagedList properties using Automapper?

0条回答
登录 后发表回答