/// This dictionary caches the delegates for each 'to-clone' type.
/// </summary>
static Dictionary<Type, Delegate> _cachedIL = new Dictionary<Type, Delegate>();
///
<summary>
/// The Main method that's executed for the test.
/// </summary>
/// <param name="args"></param>
staticvoid Main(string[] args)
{
DoCloningTest(1000);
DoCloningTest(10000);
DoCloningTest(100000);
//Commented because the test takes long <img class="emoji" draggable="false" alt="??" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f609.svg">
//DoCloningTest(1000000);
Console.ReadKey();
}
///
<summary>
/// Do the cloning test and printout the results.
/// </summary>
/// <param name="count">Number of items to clone</param>
foreach (FieldInfo field in myObject.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic))
{
// Load the new object on the eval stack... (currently 1 item on eval stack)
/// Clone a person object by manually typing the copy statements.
/// </summary>
/// <param name="p">Object to clone</param>
/// <returns>Cloned object</returns>
privatestatic Person CloneNormal(Person p)
{
Person newPerson = new Person();
newPerson.ID = p.ID;
newPerson.Name = p.Name;
newPerson.FirstName = p.FirstName;
return newPerson;
}
}
Different ways of Object Cloning
Object Cloning is broadly categorized into Shallow copy and Deep copy. This article is not about shallow Copy and deep copy so will just give a brief introduction.
Shallow Copy
Copy the reference but not referenced object.
Deep Copy
Copy the reference and referenced objects.
Multiple ways to create clone of Object,
Clone Manually (ICloneable Interface) This is simple but not ra ecommended way to create object as it does not specify if the object cloning is done by Deep copy approach or Shallow copy approach.
/// This dictionary caches the delegates for each 'to-clone' type.
/// </summary>
static Dictionary<Type, Delegate> _cachedIL = new Dictionary<Type, Delegate>();
///
<summary>
/// The Main method that's executed for the test.
/// </summary>
/// <param name="args"></param>
staticvoid Main(string[] args)
{
DoCloningTest(1000);
DoCloningTest(10000);
DoCloningTest(100000);
//Commented because the test takes long <img class="emoji" draggable="false" alt="??" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f609.svg">
//DoCloningTest(1000000);
Console.ReadKey();
}
///
<summary>
/// Do the cloning test and printout the results.
/// </summary>
/// <param name="count">Number of items to clone</param>
foreach (FieldInfo field in myObject.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic))
{
// Load the new object on the eval stack... (currently 1 item on eval stack)
/// Clone a person object by manually typing the copy statements.
/// </summary>
/// <param name="p">Object to clone</param>
/// <returns>Cloned object</returns>
privatestatic Person CloneNormal(Person p)
{
Person newPerson = new Person();
newPerson.ID = p.ID;
newPerson.Name = p.Name;
newPerson.FirstName = p.FirstName;
return newPerson;
}
}
Clone with Extension Methods I found this blog and it’s great. I am providing the link here.
P.S.
There is nothing much new in this article here asI already found similar writing online. I hope this article enlarges the scope of its original article .This article I will keep for my personal knowledge repository
Join the conversation! Your thoughts help the community grow.