在向類添加新的可選字段之後,此類的先前序列化的實例不再可反序列化。使用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上標記爲保留,嘗試在沒有它的情況下運行代碼。 –