Avatar of Marc Davis
Marc Davis
Flag for United States of America

asked on 

Copy nested objects of an object to another of the same type.

I am trying to copy the properties from one object and this code works absolutely great with one exception.  If there is another object with the object (i.e. nested) then it will not copy the nested.

Here is the same which was posted on another location:

    public static class Reflection
    {
        /// <summary>
        /// Extension for 'Object' that copies the properties to a destination object.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="destination">The destination.</param>
        public static void CopyProperties(this object source, object destination)
        {
            // If any this null throw an exception
            if (source == null || destination == null)
                throw new Exception("Source or/and Destination Objects are null");
            // Getting the Types of the objects
            Type typeDest = destination.GetType();
            Type typeSrc = source.GetType();
            // Collect all the valid properties to map
            var results = from srcProp in typeSrc.GetProperties()
                          let targetProperty = typeDest.GetProperty(srcProp.Name)
                          where srcProp.CanRead
                          && targetProperty != null
                          && (targetProperty.GetSetMethod(true) != null && !targetProperty.GetSetMethod(true).IsPrivate)
                          && (targetProperty.GetSetMethod().Attributes & MethodAttributes.Static) == 0
                          && targetProperty.PropertyType.IsAssignableFrom(srcProp.PropertyType)
                          select new { sourceProperty = srcProp, targetProperty = targetProperty };
            //map the properties
            foreach (var props in results)
            {
                props.targetProperty.SetValue(destination, props.sourceProperty.GetValue(source, null), null);
            }
        }
    }

Open in new window


Does anybody know how to get the nested objects that is in the source with modifying the above LINQ?

What I am talking about is let's say, for example there is a Person object and that person object has like a Employer and there is a nested object of the Employer Location...something like:

Person
   - FirstName
   - LastName
   - Sex
   - Age
   - House Addr
   - City
   - State
   - Zip
   - Employer (this is where the nested starts)
          - Employer House Addr (nested Employer Object)
          - Employer City (nested Employer object)
          - Employer State (nested Employer object)
          - Employer Zip  ((nested Employer object)
   - Car
   - Family

The above code will copy everything BUT when it gets to the Employer nested object it does not get anything and copies it as a null.

How can that above be modified also incorporate the nested object?

Any assistance on that would be absolutely greatly appreciated!
* LINQ.NET ProgrammingC#

Avatar of undefined
Last Comment
Marc Davis

8/22/2022 - Mon