Ejemplo n.º 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="sourceList">   The source List. </param>
        /// <param name="notUseCache"></param>
        ///
        /// <returns>   A TDest. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static List <TDest> MapList <TSource, TDest>(List <TSource> sourceList, bool notUseCache = false)
            where TSource : class
            where TDest : class, new()
        {
            var resultList = new List <TDest>();

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

            Type typeDest   = typeof(TDest);
            Type typeSource = typeof(TSource);

            var list = new List <string>();

            list = (notUseCache ?
                    SimpleTypeHelper.GetSameProperties(typeDest, typeSource)
                : SimpleTypeHelper.GetSamePropertiesFromDict(typeDest, typeSource)) ?? new List <string>();

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

                SetInstanceValues(source, dest, list);

                resultList.Add(dest);
            });

            return(resultList);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Map Property Values to another type instance.
        /// </summary>
        /// <typeparam name="TSource">Source Generic Type</typeparam>
        /// <typeparam name="TDest">Destination Generic Type</typeparam>
        /// <param name="source">Source generic type instance</param>
        /// <param name="instance">Destination generic type instance</param>
        /// <param name="notUseCache"></param>
        public static void MapTo <TSource, TDest>(TSource source,
                                                  TDest instance, bool notUseCache = false)
            where TSource : class
            where TDest : class
        {
            if (source == null)
            {
                return;
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            Type typeDest   = typeof(TDest);
            Type typeSource = typeof(TSource);

            var list = new List <string>();

            list = (notUseCache ?
                    SimpleTypeHelper.GetSameProperties(typeDest, typeSource)
                : SimpleTypeHelper.GetSamePropertiesFromDict(typeDest, typeSource)) ?? new List <string>();

            SetInstanceValues(source, instance, list);
        }
Ejemplo n.º 3
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="source">   The source to act on. </param>
        /// <param name="notUseCache">if true will be used Cache</param>
        ///
        /// <returns>   A TDest. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static TDest Map <TSource, TDest>(TSource source, bool notUseCache = false)
            where TSource : class
            where TDest : class, new()
        {
            if (source == null)
            {
                return(null);
            }

            Type typeDest   = typeof(TDest);
            Type typeSource = typeof(TSource);

            var list = new List <string>();

            list = (notUseCache ?
                    SimpleTypeHelper.GetSameProperties(typeDest, typeSource)
                : SimpleTypeHelper.GetSamePropertiesFromDict(typeDest, typeSource)) ?? new List <string>();

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

            SetInstanceValues(source, dest, list);

            return(dest);
        }