Deep cloning in Compact Framework

Is deep cloning of an object possible in a compact structure? I was hoping to use IClonable and memberwiseclone (), but this only does a shallow copy.

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

Many thanks,

Morris

+3
source share
1 answer

I implemented a deep copy of the object, 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);
  }
}
+6
source

Source: https://habr.com/ru/post/1721934/


All Articles