Exemple #1
0
        // --------------------------------------------------------------------------------------------------------------------------
        public static T CreateCopy <T>(T source) where T : new()
        {
            T res = Activator.CreateInstance <T>();

            DTOMapper.CopyMembers(source, res);
            return(res);
        }
Exemple #2
0
        // --------------------------------------------------------------------------------------------------------------------------
        private static void CopyListDataEx <TFrom, TTo>(TFrom from, TTo to, DataMember fromMember, DataMember toMember, Type listType1, Type listType2)
        {
            IList sourceList = (IList)fromMember.GetValue(from);
            IList target     = (IList)toMember.GetValue(to);

            if (target == null)
            {
                if (toMember.CanWrite)
                {
                    target = (IList)Activator.CreateInstance(toMember.DataType, null);
                    toMember.SetValue(to, target);
                }
            }
            else
            {
                target.Clear();
            }

            if (sourceList != null)
            {
                if (ReflectionTools.IsSimpleType(listType1))
                {
                    foreach (var item in sourceList)
                    {
                        target.Add(item);
                    }
                }
                else
                {
                    foreach (var item in sourceList)
                    {
                        object nextItem = DTOMapper.CreateCopy(listType1, listType2, item);
                        target.Add(nextItem);
                    }
                }
            }
            else
            {
                // We can't directly write to the list (which is how we got here) so we will just clear it out instead.
                if (target != null)
                {
                    target.Clear();
                }
            }
        }