1

我得到了一个类型化(未连接)的数据集,以及使用该数据集创建的许多记录(二进制序列化)。我已经为其中一种类型添加了一个属性,我想用新数据集转换旧记录。我知道如何加载它们:使用旧模式 dll 为 BinaryFormatter 提供自定义绑定器。问题是如何将旧类型的对象转换为新类型的对象 - 两种类型具有相同的名称,但新类型具有更多属性。

4

2 回答 2

2

If the only difference between the existing dataset and the new one is an added field then you can "upgrade" them by writing out the old ones to XML and then reading that into the new ones. The value of the added field will be DBNull.

MyDataSet myDS = new MyDataSet();
MyDataSet.MyTableRow row1 = myDS.MyTable.NewMyTableRow();
row1.Name = "Brownie";
myDS.MyTable.Rows.Add(row1);

MyNewDataSet myNewDS = new MyNewDataSet();

using(MemoryStream ms = new MemoryStream()){
    myDS.WriteXml(ms);
    ms.Position = 0;
    myNewDS.ReadXml(ms);
}
于 2008-09-17T08:49:25.777 回答
0

你能让新类继承旧类吗?如果是这样,也许您可​​以通过强制转换简单地反序列化为新的。

如果不是,另一种可能的解决方案是实现批处理操作,其中您在不同的命名空间中包含对旧类和新类的引用,水合旧对象,对新类的对象执行深度复制,并序列化新对象.

于 2008-09-17T08:42:23.110 回答