反序列化旧文件的问题。
我有一个类:
然后有个泛类对象:
public StockList<KeywordOptions> MyKeywords { get; set; }
this.MyKeywords = new StockList<KeywordOptions>();
....
然后将MyKeywords 用BinaryFormatter序列化为一个文件myKeywords.dat,
现在我需要扩展KeywordOptions类(增加几个属性):
编译后我的程序要反序列化原来的myKeywords.dat文件,但这时候已经无法转换成新的KeywordOptions对像了。
有没有什么解决方法?
[Serializable()]
public class KeywordOptions
{
/// <summary>
/// 关键字
/// </summary>
public string Keyword { get; set; }
/// <summary>
/// 是否为系统定义的关键字
/// </summary>
public bool Defined { get; set; }
}
然后有个泛类对象:
public StockList<KeywordOptions> MyKeywords { get; set; }
this.MyKeywords = new StockList<KeywordOptions>();
....
然后将MyKeywords 用BinaryFormatter序列化为一个文件myKeywords.dat,
现在我需要扩展KeywordOptions类(增加几个属性):
[Serializable()]
public class KeywordOptions
{
/// <summary>
/// 关键字
/// </summary>
public string Keyword { get; set; }
/// <summary>
/// 是否为系统定义的关键字
/// </summary>
public bool Defined { get; set; }
/// <summary>
/// 是否已选中做为搜索关键字配置
/// </summary>
public bool Selected { get; set; }
/// <summary>
/// 消息发布的日期是几天内的(默认 1天)
/// </summary>
public int Days { get; set; }
}
编译后我的程序要反序列化原来的myKeywords.dat文件,但这时候已经无法转换成新的KeywordOptions对像了。
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
FileStream fs = new FileStream(@"c:\\myKeywords.dat", FileMode.Open, FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
using (fs)
{
this.MyKeywords = (StockList<KeywordOptions>)bf.Deserialize(fs);// 这里会出错
fs.Close();
}
有没有什么解决方法?
...全文
相关推荐
java对象序列化与反序列化原理解析
`serialVersionUID`是保证序列化兼容性的关键,正确理解和使用它能够避免反序列化时因类结构改变而产生的问题。
在Unity5中Protobuf的序列化和反序列化
- 注意序列化与反序列化的版本兼容性,升级数据结构时应考虑到旧数据的解析。 - 使用预编译的ProtoBuf服务,以提高运行时性能。 - 考虑错误处理,确保在网络通信不稳定或数据损坏时能妥善处理。
系列化和反序列化的一个文砀
**配置文件**:配置文件通常以序列化的形式存在,便于阅读和修改。在实际操作中,需要注意以下几个问题:1. **安全性**:序列化可能会暴露敏感信息,因此需要谨慎处理。
weblogic-CEV反序列化补丁p28483404安装升级方法安装升级指导
### WebLogic-CEV 反序列化漏洞及P28483404补丁安装升级指南#### 一、WebLogic-CEV反序列化漏洞简介WebLogic-CEV反序列化漏洞是一种常见的高危漏洞,该漏洞允许攻击者通过远程执行恶意代码或者上传文件等方式对系统造成威胁
外包20220324相关文件
Python的pickle模块,用于对象的序列化和反序列化。3. 数据存储和复用的概念,通过pickle文件保存和加载数据。4. 数据分析上下文,文件名暗示了因子数据可能与问题解决或建模相关。5.


