Advertisement
Guest User

AutoMapperExtensions

a guest
Jun 29th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1.     public static class AutoMapperExtensions
  2.     {
  3.         /// <summary>
  4.         /// Map an IEnumerable type to a list of result type
  5.         /// </summary>
  6.         /// <typeparam name="TResult"></typeparam>
  7.         /// <param name="self"></param>
  8.         /// <returns></returns>
  9.         public static List<TResult> MapTo<TResult>(this IEnumerable self)
  10.         {
  11.             if (self == null)
  12.                 throw new ArgumentNullException();
  13.  
  14.             return (List<TResult>)Mapper.Map(self, self.GetType(), typeof(List<TResult>));
  15.         }
  16.  
  17.         /// <summary>
  18.         /// Map one object to a new instance of a specific type
  19.         /// </summary>
  20.         /// <typeparam name="TResult"></typeparam>
  21.         /// <param name="self"></param>
  22.         /// <returns></returns>
  23.         public static TResult MapTo<TResult>(this object self)
  24.         {
  25.             if (self == null)
  26.                 throw new ArgumentNullException();
  27.  
  28.             return (TResult)Mapper.Map(self, self.GetType(), typeof(TResult));
  29.         }
  30.  
  31.         /// <summary>
  32.         /// Map one object to a specific instance
  33.         /// </summary>
  34.         /// <typeparam name="TResult"></typeparam>
  35.         /// <param name="self">Object to map from</param>
  36.         /// <param name="destination">Destination object to map to</param>
  37.         /// <returns></returns>
  38.         public static TResult MapTo<TResult>(this object self, TResult destination)
  39.         {
  40.             if (self == null)
  41.                 throw new ArgumentNullException("self");
  42.             if (destination == null)
  43.                 throw new ArgumentNullException("destination");
  44.  
  45.             return (TResult)Mapper.Map(self, destination, self.GetType(), typeof(TResult));
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Map one object from a specific instance
  50.         /// </summary>
  51.         /// <typeparam name="TResult"></typeparam>
  52.         /// <param name="self">Object to map to</param>
  53.         /// <param name="source">Source object to map from</param>
  54.         /// <returns></returns>
  55.         public static TResult MapFrom<TResult>(this TResult self, object source)
  56.         {
  57.             if (self == null)
  58.                 throw new ArgumentNullException("destination");
  59.             if (source == null)
  60.                 throw new ArgumentNullException("source");
  61.  
  62.             return (TResult)Mapper.Map(source, self, source.GetType(), typeof(TResult));
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement