troubleshooting Question

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

Avatar of davism
davismFlag for United States of America asked on
.NET ProgrammingC#* LINQ
45 Comments1 Solution129 ViewsLast Modified:
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);
            }
        }
    }

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!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 45 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 45 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros