我有一个bellow函数,它返回一个动态对象列表
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 类列表。 任何人都可以举例说明如何做到这一点?
答案 0 :(得分: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();