0

I am using the code from Apply properties values from one object to another of the same type automatically?

 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();

        // Iterate the Properties of the source instance and  
        // populate them from their desination counterparts  
        PropertyInfo[] srcProps = typeSrc.GetProperties();
        foreach (PropertyInfo srcProp in srcProps)
        {
            if (!srcProp.CanRead)
            {
                continue;
            }
            PropertyInfo targetProperty = typeDest.GetProperty(srcProp.Name);
            if (targetProperty == null)
            {
                continue;
            }
            if (!targetProperty.CanWrite)
            {
                continue;
            }
            if (targetProperty.GetSetMethod(true) != null && targetProperty.GetSetMethod(true).IsPrivate)
            {
                continue;
            }
            if ((targetProperty.GetSetMethod().Attributes & MethodAttributes.Static) != 0)
            {
                continue;
            }
            if (!targetProperty.PropertyType.IsAssignableFrom(srcProp.PropertyType))
            {
                continue;
            }
            // Passed all tests, lets set the value
            targetProperty.SetValue(destination, srcProp.GetValue(source, null), null);
        }
    }


}

That all works great!

What am having trouble trying to figure out is how to create a similar function that takes a List of source and copy to a List of destination and use that to call the code above.

Of course this doesn't work but Something like:

 public static void CopyListProperties(this List<object> sourceList, List<object> destinationList)
    {

        foreach (var item in sourceList)
        {
            var destinationObject = new destinationObjectType();
            item.CopyProperties(destinationObject);
            destinationList.Add(destinationObject);
        }

    }
Community
  • 1
  • 1
bhs8227
  • 81
  • 9
  • "Pasting code from the Internet into production code is like chewing gum found in the street." - Mike Johnson : Use Generics for default constructor so code woud be like --- CopyListProperties(this List sourceList, List destinationList) where T: new() .... var destinationObject = new T(); – Alex Krupka Aug 10 '16 at 17:33
  • @Alex Krupka That's really helpful. Thanks! I don't remember saying it was being used in production code? – bhs8227 Aug 10 '16 at 17:41
  • No but the point stands. If you don't understand how code is working you should not be copying it willy nilly into your code. It will come back to bite you. – Alex Krupka Aug 10 '16 at 17:42
  • @AlexKrupka Thank you. I had tried a slew of combinations including the answer in your comment but the one part I was ultimately missing was the new constraint. The function that was copied is pretty straight forward reflection. I was having issues with generic Types. I do appreciate your help and will update my post with your answer. – bhs8227 Aug 10 '16 at 18:25

1 Answers1

1

Thanks to Alex on this one.

Here is the function how it should be.

public static void CopyListProperties<T>(this List<object> sourceList, List<T> destinationList) where T: new()
        {

            foreach (var item in sourceList)
            {
                var destinationObject = new T();
                item.CopyProperties(destinationObject);
                destinationList.Add(destinationObject);
            }

        }
bhs8227
  • 81
  • 9