Copying Values Among Objects

I was writing a code when Chris Gale was batting. A Century in just 30 balls, that's some awesome batting man! So you can imagine the importance of that code.

Well that code was all written to copy the values of one object to another, property by property. The good thing is, the two objects need not be the same! Another good thing is that we do not need to even bother what the properties are that the objects possess. Aren't you amazed? Let me show you how.

When we talk about classes, we are talking about something that is a reference type; in other words when you create two instances of that class (inc1 and inc2, say) and initialize the second instance with the first (inc2 = inc1), the two instances will now be sharing a common memory location. That is to say, if you change the value of the first instance (inc1 in our case), the second instance (inc2) will automatically reflect that change. It is something like you and your friend both write on one paper; no matter who writes, the content will change for both of you.

This referencing is good sometimes, since it saves memory, but situations do exist when you need to intentionally create two separate objects of the same class. You still want both objects to have the same propertt values. You can't simply just equate the two since that will create a reference. There is a way though, in which you equate two objects by individually assigning the properties of one to another. But that situation can become even worse if the object has hundreds of properties to work with. Assigning hundreds of properties is not a really good idea, don't you think?

What next? Well there is a solution. What if you can, somehow, get all the properties of the objects into collections. Then you can use one property of the source object, search the same property in the destination object, then assign the value of the property of the source object to the same property of the destination object. Good idea, ain't it? So, let's get started. The following is the function that does exactly as we've discussed above.

public void CopyObject(object source, object destination)
{
    var propertyOfDestinationObject = destination.GetType().GetProperties();
    var propertyOfSourceObject = source.GetType().GetProperties();
    foreach (var itemSource in propertyOfSourceObject)
    {
        foreach (var itemDestination in propertyOfDestinationObject)
        {
            if (itemDestination.Name.Equals(itemSource.Name))
            {
                itemDestination.SetValue(destination, itemSource.GetValue(source, new object[] {}),
                new object[] {});
                break;
            }
        }  
    }
}

As you can see, we first created the list of all the properties of the source and destination objects using "propertyOfSourceObject" and "propertyOfDestinationObject". It grabs one property from the source and loops through the entire properties of the destination, to search for that specific property. Once found, the value of the source property is assigned to the destination property. This process will continue until all the properties of the source value have been copied to their counterparts in the destination. Once completed, bang, we are done copying.


Similar Articles