MappingExtensions.cs 892 B

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using AutoMapper;
  6. using AutoMapper.Mappers;
  7. namespace Bowin.Common.Mapping
  8. {
  9. public static class MappingExtensions
  10. {
  11. public static T CloneTo<T>(this T obj, T newObj)
  12. {
  13. Mapper.CreateMap<T, T>();
  14. Mapper.DynamicMap<T, T>(obj, newObj);
  15. return newObj;
  16. }
  17. public static void DynamicCloneTo<TSource, TDestination>(this TSource source, TDestination destination)
  18. {
  19. Mapper.DynamicMap(source,destination);
  20. }
  21. public static T To<T, TType>(this IEnumerable<TType> objects) where T : IList<TType>, new()
  22. {
  23. if (objects == null) return default(T);
  24. var tResult = new T();
  25. foreach (var s in objects) tResult.Add(s);
  26. return tResult;
  27. }
  28. }
  29. }