Mor*_*lgn 3 c# compact-framework clone windows-mobile
是否有可能在紧凑框架中深度克隆对象?我希望使用IClonable和memberwiseclone()但是这只执行浅拷贝.
有关如何使用C#2.0的任何想法吗?
非常感谢,
莫里斯
我已经通过使我的对象可序列化[Serializable()]并使用以下方法实现了深层对象副本.
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);
}
}
Run Code Online (Sandbox Code Playgroud)