Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep cloning in Compact Framework

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

like image 535
Morrislgn Avatar asked Nov 26 '25 00:11

Morrislgn


1 Answers

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);
  }
}
like image 61
stevehipwell Avatar answered Nov 27 '25 15:11

stevehipwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!