views:

103

answers:

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?

+1  A: 
 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

ArsenMkrt
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