views:

131

answers:

1

Hi Folks,

Is it possible to deep clone an object in the compact framework? I was hoping to use IClonable and memberwiseclone() however this only performs a shallow copy.

Any ideas on how to do this please using C# 2.0?

Many thanks,

Morris

+2  A: 

I've implemented a deep object copy by making my objects serializable [Serializable()] and using the following method.

public static ObjectType CopyObject<ObjectType>(ObjectType oObject)
{
  XmlSerializer oSeializer = null;

  //Creates the serializer
  oSeializer = new XmlSerializer(oObject.GetType);

  //Use the stream
  using (MemoryStream oStream = new MemoryStream())
  {
    //Serialize the object
    oSeializer.Serialize(oStream, oObject);

    //Set the strem position
    oStream.Position = 0;

    //Return the object
    return (ObjectType)oSeializer.Deserialize(oStream);
  }
}
Stevo3000
Folks,Thanks for your advice, much appreciated. CheersMorris
Morrislgn