C#ISerializable问题
我打算用序列化做克隆.我必须让我的课程ISerializable.但是它的超级类和所有引用的变量类呢?我需要将它们全部变为ISerializable吗?
如果我使用ISerializable.我必须实现GetObjectData(),我应该放在该方法中?把它留空是好的吗? 解决方法
GetObjectData()允许您控制序列化过程.
例: public Employee(SerializationInfo info,StreamingContext ctxt) { //Get the values from info and assign them to the appropriate properties EmpId = (int)info.GetValue("EmployeeId",typeof(int)); EmpName = (String)info.GetValue("EmployeeName",typeof(string)); } public void GetObjectData(SerializationInfo info,StreamingContext ctxt) { //You can use any custom name for your name-value pair. But make sure you // read the values with the same name. For ex:- If you write EmpId as "EmployeeId" // then you should read the same with "EmployeeId" info.AddValue("EmployeeId",EmpId); info.AddValue("EmployeeName",EmpName); } 上面的示例显示了如何反序列化和序列化.如您所见,反序列化需要GetObjectData.如果将其留空,则您的对象将没有所需的值. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |