I’m trying to perform a deep copy on a custom data structure. My problem is that the array (object[]) that holds the data I want to copy is of many different types (string, System.DateTime, custom structures etc). Doing the following loop will copy an object’s reference, so any changes made in one object will reflect in the other.
for (int i = 0; i < oldItems.Length; ++i)
{
newItems[i] = oldItems[i];
}
Is there a generic way to create new instances of these objects, and then copy any of the values into them?
P.s. must avoid 3rd party libraries
Assuming Automapper is out of the question (as @lazyberezovsky noted in his answer), you can serialize it for the copy:
Note though: this would all be horribly inefficient.. surely there’s a better way to do what you’re trying to do.