在向类添加新的可选字段之后,此类的先前序列化的实例不再可反序列化。使用OptionalField的反序列化错误
假设我有救使用BinaryFormatter的MyClass的一些实例:
[Serializable]
public class MyClass
{
public MyType A;
}
后,MyClass的第二次修改:
[Serializable]
public class MyClass
{
public MyType A;
[OptionalField(VersionAdded = 2)]
public MyType NewField;
}
现在年纪大物体不再deserializable。堆栈跟踪我尝试反序列化时,他们得到的是以下(配置文件是.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)
我无法找到互联网或类似的堆栈跟踪这个堆栈跟踪。请注意,与Mono一起运行该软件的相同文件是可读的;-)。正因为如此,我猜想这个问题可能与.NET错误有关。
你究竟如何反序列化它呢?你可以添加代码吗? –
VersionAdded属性在.NET 4上标记为保留,尝试在没有它的情况下运行代码。 –