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# 开发者_JAVA技巧2.0?
Many thanks,
Morris
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);
}
}
精彩评论