THE ULTIMATE IN COPYING/CLONING OBJECTS!!!!
Create yourself a nice abstract base class, which includes the ICLONABLE interface. Then include this code below in your base class. Derive all of your domain objects from there. You could even try a nice abstract base collection class and also include this code below. Be creative, but always use this code on your base classes. Don't forget to make them SERIALIZABLE. This will clone datatables, arrays, custom collections, simple properties, hashtables... whatever you want. Why add code to all of your classes when this will do it all!
public object Clone(bool doDeepCopy)
{
if (doDeepCopy)
{
BinaryFormatter BF = new BinaryFormatter();
MemoryStream memStream = new MemoryStream();
BF.Serialize(memStream, this);
memStream.Flush();
memStream.Position = 0;
return (BF.Deserialize(memStream));
}
else
{
return (this.MemberwiseClone());
}
}
public object Clone()
{
return (Clone(false));
}
Gordon.