Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }