클래스에 새 선택적 필드를 추가 한 후에이 클래스의 이전에 serialize 된 인스턴스는 더 이상 deserializable이 불가능합니다.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됩니다. 내가 인터넷 또는 유사한 스택 추적에이 스택 트레이스를 찾을 수 없습니다
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)
:를 역 직렬화 할 때 내가 얻을 스택 추적은 다음 (프로파일은 .NET 4.0)입니다. Mono ;-)로 소프트웨어를 실행하면서 동일한 파일을 읽을 수 있습니다. 이 때문에이 문제는 .NET 버그와 관련 될 수 있다고 생각합니다.
정확히 역 직렬화 하시겠습니까? 이 코드를 추가 할 수 있습니까? –
VersionAdded 속성은 .NET 4에서 예약 된 것으로 표시되어 있습니다. 코드없이 코드를 실행 해보십시오. –