在我向类添加新的可选字段后,该类的先前序列化实例不再可反序列化。
假设我使用 BinaryFormatter 保存了一些 MyClass 实例:
[Serializable]
public class MyClass
{
public MyType A;
}
之后,MyClass 的第二次修订:
[Serializable]
public class MyClass
{
public MyType A;
[OptionalField(VersionAdded = 2)]
public MyType NewField;
}
现在旧对象不再可反序列化。我在尝试反序列化它们时获得的堆栈跟踪如下(配置文件是 .NET 4.0):
System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Reflection.Assembly.GetAssembly(Type type)
at System.Runtime.Serialization.Formatters.Binary.BinaryConverter.GetParserBinaryTypeInfo(Type type, Object& typeInformation)
at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, Type objectType, String[] memberNames, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMap(BinaryObjectWithMap record)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck)
我在 Internet 上找不到此堆栈跟踪或类似的堆栈跟踪。请注意,使用 Mono 运行软件时,相同的文件是可读的;-)。因此,我认为该问题可能与 .NET 错误有关。