1

I'm implementing the IEditableObject interface and I want to make a generic method that will know how to clone the object before BeginEdit().

I thought about reflection to iterate all public properties and copy them to a cached object.

Anyone have a better idea?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141

1 Answers1

1
 public object Clone()
 {
     DataContractSerializer serializer = new DataContractSerializer(this.GetType());
     using (MemoryStream memStream = new MemoryStream())
     {
         serializer.WriteObject(memStream, this);
         memStream.Position = 0;
         return serializer.ReadObject(memStream);
     }
  }

Above is generic clone method,use that if you know your object is datacontract serializable, or if xml serializable you can use XmlSerializer

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • and is there a generic method that will copy old values that i saved to the existing objec, i dont wanna change his reference!!! so what can i do if i want to roll him back? – Chen Kinnrot Jul 30 '09 at 13:17