/// <summary> /// Maps the specified source. /// </summary> /// <typeparam name="TSource">The type of the source.</typeparam> /// <typeparam name="TTarget">The type of the dest.</typeparam> /// <param name="source">the source object.</param> /// <param name="name">The name.</param> /// <returns> /// A new object of <typeparamref name="TTarget"></typeparamref> /// </returns> public static TTarget Map <TSource, TTarget>(TSource source, string name = null) where TSource : class where TTarget : class { // No source, no target. if (source == null) { return(null); } TTarget result = null; try { MapperConfiguration <TSource, TTarget> mapper = GetMapper <TSource, TTarget>(name); Func <TSource, TTarget> query = mapper.GetFuncDelegate(); if (query != null) { result = query(source); // Action performed after the mapping mapper.ExecuteAfterActions(source, result); } } catch (Exception) { throw; } return(result); }
/// <summary> /// 将指定的源映射到目标。 /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TDest">目标类型</typeparam> /// <param name="source">源对象</param> /// <param name="target">目标对象</param> /// <param name="name">别名</param> public static void Map <TSource, TDest>(this TSource source, TDest target, string name = null) where TSource : class where TDest : class { if (!_initialized) { Initialize(); } TDest result = null; MapperConfiguration <TSource, TDest> mapper = GetMapper <TSource, TDest>(name); Action <TSource, TDest> query = mapper.GetDelegateForExistingTarget() as Action <TSource, TDest>; if (query != null) { query(source, target); // 映射后执行的操作 mapper.ExecuteAfterActions(source, result); } }
/// <summary> /// 映射指定的源。 /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TDest">目标类型</typeparam> /// <param name="source">源对象</param> /// <param name="name">别名</param> /// <returns> /// 目标对象的新实例 /// </returns> public static TDest Map <TSource, TDest>(this TSource source, string name = null) where TSource : class where TDest : class { if (source == null) { return(null); } if (!_initialized) { Initialize(); } TDest result = null; MapperConfiguration <TSource, TDest> mapper = GetMapper <TSource, TDest>(name); Func <TSource, TDest> query = mapper.GetFuncDelegate(); if (query != null) { result = query(source); // 映射后执行的操作 mapper.ExecuteAfterActions(source, result); } return(result); }
/// <summary> /// Maps the specified source to the target. /// </summary> /// <typeparam name="TSource">The type of the source.</typeparam> /// <typeparam name="TTarget">The type of the target.</typeparam> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="name">The name.</param> public static void Map <TSource, TTarget>(TSource source, TTarget target, string name = null) where TSource : class where TTarget : class { Contract.Requires(source != null); Contract.Requires(target != null); TTarget result = null; try { MapperConfiguration <TSource, TTarget> mapper = GetMapper <TSource, TTarget>(name); Action <TSource, TTarget> query = mapper.GetDelegateForExistingTarget() as Action <TSource, TTarget>; if (query != null) { query(source, target); // Action performed after the mapping mapper.ExecuteAfterActions(source, result); } } catch (Exception) { throw; } }