[Sample Of Dec 9th] How to deep clone objects using reflection in .NET

 

Homepage image
RSS Feed

Sample Download :

CS Version: https://code.msdn.microsoft.com/CSDeepCloneObject-8a53311e

VB Version: https://code.msdn.microsoft.com/VBDeepCloneObject-79e5a41f

This sample demonstrates how to implement deep clone between objects in .NET using reflection.

We can use the MemberwiseClone to get a copy, but the MemberwiseClone method creates a shallow copy by creating a new object, and then copying the non-static fields of the current object to the new object. If a field is a value type, a   bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not.

In this sample, we make use metadata information to clone a new object and drill down each field, ultimately, copy this field.
image

You can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.