In .NET I want to make a deep copy of an Object. But how deep is exactly deep?
For example, I have a SalesOrder Class. I could deep copy it by creating a new SalesOrder Object and copy all value types into the new Object. However, a SalesOrder has a collection of SalesOrderDetails. Luck has it that the SalesOrderDetail Class does not have a DeepClone Method, so I will have to copy every SalesOrderDetail Object in the SalesOrder... Here comes the fun part, every SalesOrderDetail has a Product, which also does not have a DeepClone Method (are there ANY .NET Classes that have this Method?).
In the case of, for example, Entity Framework every Product has a list of SalesOrderDetails which again have a SalesOrder and the circle is complete... Entity Framework uses lazy loading to not load an entire database into your application at once. But when you're going to clone you effectively need to call every Object in case it exists and needs to be cloned.
I've Googled around and I've seen many people saying Serialization is the easiest way to create a deep copy of an Object, but my experience is that many (non-custom made) Classes cannot be Serialized just like that (circular references etc.) if they can be serialized at all.
So how deep should a deep copy actually go and is it even possible to create a perfect deep copy of a more complex Object? I guess Reflection might take you pretty far, but this too is far from ideal (although preferred above Serialization for me).
Any thoughts?
Thanks.