Exemple #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   A TSource extension method that maps the given source. </summary>
        ///
        /// <remarks>   Msacli, 22.04.2019. </remarks>
        ///
        /// <typeparam name="TSource">  Type of the source. </typeparam>
        /// <typeparam name="TDest">    Type of the destination. </typeparam>
        /// <param name="propertyMap">   Property Map string, like SourceProp1:DestProp1;SourceProp2:DestProp2;SourceProp3:DestProp3. </param>
        /// <param name="sourceList">   The source List. </param>
        ///
        /// <returns>   A TDest. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static List <TDest> Map <TSource, TDest>(List <TSource> sourceList, string propertyMap)
            where TSource : class
            where TDest : class, new()
        {
            if (string.IsNullOrWhiteSpace(propertyMap))
            {
                return(MapList <TSource, TDest>(sourceList, notUseCache: false));
            }

            var resultList = new List <TDest>();

            if (sourceList == null || sourceList.Count < 1)
            {
                return(resultList);
            }

            Type typeDest   = typeof(TDest);
            Type typeSource = typeof(TSource);
            var  dictionary = new Dictionary <string, string>();

            dictionary =
                SimpleTypeHelper.GetKeyValues(propertyMap,
                                              InternalAppValues.FirstDelimiter, InternalAppValues.SecondDelimiter)
                ?? new Dictionary <string, string>();

            sourceList.ForEach(source =>
            {
                var dest = Activator.CreateInstance <TDest>();

                SetInstanceValues(source, dest, dictionary);
                resultList.Add(dest);
            });

            return(resultList);
        }
Exemple #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   A TSource extension method that maps the given source. </summary>
        ///
        /// <remarks>   Msacli, 22.04.2019. </remarks>
        ///
        /// <typeparam name="TSource">  Type of the source. </typeparam>
        /// <typeparam name="TDest">    Type of the destination. </typeparam>
        /// <param name="propertyMap">   Property Map string, like SourceProp1:DestProp1;SourceProp2:DestProp2;SourceProp3:DestProp3. </param>
        /// <param name="source">   The source to act on. </param>
        ///
        /// <returns>   A TDest. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static TDest Map <TSource, TDest>(TSource source, string propertyMap)
            where TSource : class
            where TDest : class, new()
        {
            if (string.IsNullOrWhiteSpace(propertyMap))
            {
                return(Map <TSource, TDest>(source, notUseCache: false));
            }

            if (source == null)
            {
                return(null);
            }

            Type typeDest   = typeof(TDest);
            Type typeSource = typeof(TSource);
            var  dictionary = new Dictionary <string, string>();

            dictionary = SimpleTypeHelper.GetKeyValues(propertyMap,
                                                       InternalAppValues.FirstDelimiter, InternalAppValues.SecondDelimiter) ?? new Dictionary <string, string>();

            var dest = Activator.CreateInstance <TDest>();

            SetInstanceValues(source, dest, dictionary);

            return(dest);
        }