0

I have an entity which I use to bind data with my WPF UI. I am in need to create a copy of the entity which I can use as "original" data anytime.

Just creating new object and then assigning also carries references with it. So I need a copy of entity object which has no effect of changes made on its source.

My entity contains value type properties and several nested collections.

Any suggestions/ideas on this?

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
Arpit Khandelwal
  • 1,743
  • 3
  • 23
  • 34
  • possible duplicate of [How do you do a deep copy an object in .Net (C# specifically)?](http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically) – jason Feb 18 '11 at 12:43
  • Yes, it is kind of a dupe but all the answers here seem to use BinaryFomatter. – H H Feb 18 '11 at 12:50
  • The term is "Deep Copy" and better state "without using serialization". – H H Feb 18 '11 at 12:51

3 Answers3

0

You will need to write your own copy constructor: This shows how. http://msdn.microsoft.com/en-us/library/ms173116(v=vs.80).aspx

For collections you will need to copy the data too. Array.Copy will work for most for A hashtable you might need to go as far as Serialization or simply recreating the table.

SomeType[] myArray = new SomeType[orig.Count+ 1];

orig.CopyTo(myArray, 0);
madmik3
  • 6,975
  • 3
  • 38
  • 60
0

Deep Copy in C#

Community
  • 1
  • 1
Novice
  • 2,447
  • 3
  • 27
  • 33
0

Use this project: https://github.com/havard/copyable

jgauffin
  • 99,844
  • 45
  • 235
  • 372