1. public class Source
  2. {
  3. public int Value { get; set; }
  4. }
  5. public class Destination
  6. {
  7. public int Value { get; set; }
  8. }
  9. var configuration = new MapperConfiguration(cfg => cfg.CreateMap<Source, Destination>());
  10. var sources = new[]
  11. {
  12. new Source { Value = 5 },
  13. new Source { Value = 6 },
  14. new Source { Value = 7 }
  15. };
  16. IEnumerable<Destination> ienumerableDest = mapper.Map<Source[], IEnumerable<Destination>>(sources);
  17. ICollection<Destination> icollectionDest = mapper.Map<Source[], ICollection<Destination>>(sources);
  18. IList<Destination> ilistDest = mapper.Map<Source[], IList<Destination>>(sources);
  19. List<Destination> listDest = mapper.Map<Source[], List<Destination>>(sources);
  20. Destination[] arrayDest = mapper.Map<Source[], Destination[]>(sources);

所有支持的数据类型:

  • IEnumerable
  • IEnumerable
  • ICollection
  • ICollection
  • IList
  • IList
  • List
  • Arrays

多态类型

  1. public class ParentSource
  2. {
  3. public int Value1 { get; set; }
  4. }
  5. public class ChildSource : ParentSource
  6. {
  7. public int Value2 { get; set; }
  8. }
  9. public class ParentDestination
  10. {
  11. public int Value1 { get; set; }
  12. }
  13. public class ChildDestination : ParentDestination
  14. {
  15. public int Value2 { get; set; }
  16. }
  17. var configuration = new MapperConfiguration(c=> {
  18. c.CreateMap<ParentSource, ParentDestination>()
  19. .Include<ChildSource, ChildDestination>();
  20. c.CreateMap<ChildSource, ChildDestination>();
  21. });
  22. var sources = new[]
  23. {
  24. new ParentSource(),
  25. new ChildSource(),
  26. new ParentSource()
  27. };
  28. var destinations = mapper.Map<ParentSource[], ParentDestination[]>(sources);
  29. destinations[0].ShouldBeInstanceOf<ParentDestination>();
  30. destinations[1].ShouldBeInstanceOf<ChildDestination>();
  31. destinations[2].ShouldBeInstanceOf<ParentDestination>();