日期:2014-05-18 浏览次数:20806 次
public Class Test : IClonable { public Test() { } // deep copy in separeate memory space public object Clone() { MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, this); ms.Position = 0; object obj = bf.Deserialize(ms); ms.Close(); return obj; } }
[Serializable] public Class Test : IClonable { public Test() { } // deep copy in separeate memory space public object Clone() { MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, this); ms.Position = 0; object obj = bf.Deserialize(ms); ms.Close(); return obj; } }
------解决方案--------------------
[Serializable] public class UserInfo { private int m_ID; private string m_FirstName; private string m_LastName; private string m_Email; private string m_HomePhone; public string[] m_strArr; public UserInfo() { m_ID = 0; m_FirstName = ""; m_LastName = ""; m_Email = ""; m_HomePhone = ""; m_strArr = new string[10] { "abc1", "abc2", "abc3", "abc4", "abc5", "abc6", "abc7", "abc8", "abc9", "abc10" }; } public object Clone() { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream memStream = new MemoryStream(); formatter.Serialize(memStream, this); memStream.Position = 0; object obj = formatter.Deserialize(memStream); memStream.Close(); return obj; } public int ID { get { return m_ID; } set { m_ID = value; } } public string Email { get { return m_Email; } set { m_Email = value; } } public string LastName { get { return m_LastName; } set { m_LastName = value; } } public string FirstName { get { return m_FirstName; } set { m_FirstName = value; } } public string HomePhone { get { return m_HomePhone; } set { m_HomePhone = value; } } } //test: UserInfo info = new UserInfo(); UserInfo tmp = (UserInfo)info.Clone();
------解决方案--------------------