Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class AutoMapperExtensions
- {
- /// <summary>
- /// Map an IEnumerable type to a list of result type
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="self"></param>
- /// <returns></returns>
- public static List<TResult> MapTo<TResult>(this IEnumerable self)
- {
- if (self == null)
- throw new ArgumentNullException();
- return (List<TResult>)Mapper.Map(self, self.GetType(), typeof(List<TResult>));
- }
- /// <summary>
- /// Map one object to a new instance of a specific type
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="self"></param>
- /// <returns></returns>
- public static TResult MapTo<TResult>(this object self)
- {
- if (self == null)
- throw new ArgumentNullException();
- return (TResult)Mapper.Map(self, self.GetType(), typeof(TResult));
- }
- /// <summary>
- /// Map one object to a specific instance
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="self">Object to map from</param>
- /// <param name="destination">Destination object to map to</param>
- /// <returns></returns>
- public static TResult MapTo<TResult>(this object self, TResult destination)
- {
- if (self == null)
- throw new ArgumentNullException("self");
- if (destination == null)
- throw new ArgumentNullException("destination");
- return (TResult)Mapper.Map(self, destination, self.GetType(), typeof(TResult));
- }
- /// <summary>
- /// Map one object from a specific instance
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="self">Object to map to</param>
- /// <param name="source">Source object to map from</param>
- /// <returns></returns>
- public static TResult MapFrom<TResult>(this TResult self, object source)
- {
- if (self == null)
- throw new ArgumentNullException("destination");
- if (source == null)
- throw new ArgumentNullException("source");
- return (TResult)Mapper.Map(source, self, source.GetType(), typeof(TResult));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement