views:

416

answers:

2

I got a typed (not connected) dataset, and many records (binary seriliazed) created with this dataset. I've added a property to one of the types, and I want to convert the old records with the new data set. I know how to load them: providing custom binder for the BinaryFormatter with the old schema dll. The question is how can I convert objects of the old type to objects of the new type - both types has the same name but the new one has one more property.

A: 

Can you make the new class inherit from the old one? If so, maybe you can simply deserialize into the new one through casting.

If not, another possible solution is to implement a batch operation where you include a reference to the old class and new class in different namespaces, hydrate the old object, perform a deep copy into an object of the new class, and serialize the new object.

paulwhit
+1  A: 

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);
        }
Brownie