Automapper with inherited list

久未见 提交于 2019-12-20 07:28:39

问题


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?

来源:https://stackoverflow.com/questions/58980561/automapper-with-inherited-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!