反序列化旧文件的问题。

3tzjq 3tzjq 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我有一个类:

[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();
}


有没有什么解决方法?
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
3tzjq
C# 111,130 成员 642,540 内容