0

我有下面的函数,它返回一个动态对象列表

public List<dynamic> getlist()
        {
            List<dynamic> d = new List<dynamic>();

            d.Add(new { Name = "A", Age = 12 });
            d.Add(new { Name = "B", Age = 10 });
            d.Add(new { Name = "C", Age = 15 });
            d.Add(new { Name = "D", Age = 18 });
            d.Add(new { Name = "E", Age = 17 });

            return d;

        }

还有一堂课

 public class testclass
    {
        public string letter { get; set; }
        public Int64 frequency { get; set; }
    }

我使用 AutoMapper.5.2.0。我想将动态列表与testclass类的列表映射。谁能举例说明如何做到这一点?

4

1 回答 1

0

显然我们不能使用 MapFrom,但 ResolveUsing 有效:

Mapper.Initialize(cfg => cfg.CreateMap<dynamic, testclass>()
            .ForMember(dest => dest.letter, opt => opt.ResolveUsing(src => src.Name))
            .ForMember(dest => dest.frequency, opt => opt.ResolveUsing(src => src.Age)));

// Use the mapping
foreach (testclass a in Mapper.Map<List<testclass>>(getlist()))
{
    Console.WriteLine(a.letter);
}
Console.ReadKey();
于 2017-02-07T16:24:11.917 回答