Is it possible to De-Serialize a new Derived class using Old Binary?
- by Anand
In my project I have a class which I Serialize in Binary format to the disk.
Due to some new requirement I need to create a new class which is derived from the original class.
eg
[Serializable]
public class Sample
{
String someString;
int someInt;
public Sample()
{
}
public Sample(String _someString, int _someInt)
{
this.someInt = _someInt;
this.someString = _someString;
}
public String SomeString
{
get { return someString; }
}
public int SomeInt
{
get { return someInt; }
}
}
[Serializable]
public class DerivedSample : Sample
{
String someMoreString;
int someMoreInt;
public DerivedSample ()
: base()
{
}
public DerivedSample (String _someString, int _someInt, String _someMoreString, int _someMoreInt)
:
base(_someString, _someInt)
{
this.someMoreString = _someMoreString;
this.someMoreInt = _someMoreInt;
}
public String SomeMoreString
{
get { return someMoreString; }
}
public int SomeMoreInt
{
get { return someMoreInt; }
}
}
When I try to De serialize an old file which contains only object of Sample it works fine, in the current assembly. That means backward compatibility is there. But when I try to deserialize the file which contains object of DerivedSample using the previous version of the assembly application crashes. Which means forward compatibilty needs to be taken care off...
It it possible to say read only the base class part of the object from new version of the file?