When you have a [Serializable] class, some changes e.g. re-naming properties or fields can mean you lose data when de-serialising in the new version.
I'm searching for an automated way to detect these changes, in order to allow manual inspection of what's going on. So something that can be given two .Net assemblies, find the Serializable classes in it and detect potentially breaking changes.
If it can ignore things that are generally not a problem, e.g. adding fields/properties, that's a bonus, but not essential.
An example of the kind of refactoring that is fine for non-serializable classes, but causes major issues for serializable ones:
// version 1
[Serializable]
class A
{
private string _name;
public string Name
{
get { return _name;}
set {_name = value;}
}
}
// version 2
[Serializable]
class A
{
public string Name { get; set;}
}
I'd love a tool here that said something along the lines of
Class A - missing field _name
Class A - new property Name
Things I've considered so far:
- Find all classes with the [Serializable] attribute, create an instance and serialize it.
- Aside from issues where there isn't a default constructor, this feels like it would struggle when fields/properties are null when first created.
- Use reflection to find fields and their types within each [Serializable] class, and produce an alphabetical list.
- Won't handle custom serialization, and it feels the wrong approach
(In case the serializer itself matters - we currently use our own json-based serializer that inherits from System.Runtime.Serialization.Formatter, so it behaves the same way the BinarySerializer does)
(edit) Check whether binary serialized data matches the class which serialized it is a similar question - seems like there isn't a simple answer yet