自定义.NET序列化似乎并没有工作 [英] Custom .NET Serialization doesn't seem to work
问题描述
我使用的BinaryFormatter
序列化的类的对象 A
存储在系统::收藏集::通用::名单< A ^> ^
。我已经添加了 [Serializable接口]
标签,并在课堂上实施 ISerializable的
A
(均 GetObjectData使用
和特殊的构造函数)。反序列化时,我发现列表进行反序列化,并包含内容的相同数目的连载。然而,这些元素为空引用。
I am using BinaryFormatter
to serialize a collection of objects of class A
stored in a System::Collections::Generic::List<A^>^
. I have added the [Serializable]
tag and have implemented ISerializable
in class A
(both GetObjectData
and the special constructor). When deserializing, I find that the list is deserialized and contains the same number of elements as was serialized. However, each of these elements is a null reference.
我已经检查抛出的异常,并相信这是不是这样的。我已经检查,以确保其形式为 A(SerializationInfo中^信息,的StreamingContext上下文)的特殊构造
被称为正确的次数反序列化过程,但这些重新构造的对象没有的从反序列化的集合被引用。
I've checked for thrown exceptions and am sure that it is not the case. I have checked to ensure that the special constructor of the form A(SerializationInfo ^info, StreamingContext context)
is called the correct number of times during deserialization but these re-constructed objects are not being referenced from the deserialized collection.
我也换成了系统::收藏集::通用::名单&LT; A ^&GT; ^
与阵列&LT; A ^&GT; ^
,我仍然得到同样的结果。阵列具有元素的正确数目,但每个元素是一个空引用
I also replaced the System::Collections::Generic::List<A^>^
with array<A^>^
and I'm still getting the same results. The array has the correct number of elements but each element is a null reference.
任何人谁见过类似的问题?任何线索?
Anyone who has seen a similar problem? Any clues?
推荐答案
但问题是,任何对象称为子对象中不必被完全之后立即的GetValue
通话。在我的情况下,普通的列表
尚未完全反序列化,因此只包含空引用。我终于用 IDeserializationCallback
执行code后,对象图已被完全反序列化。
The problem was that any objects referred to within child objects need not have been completely deserialized immediately after a GetValue
call. In my case, the generic List
had not yet been completely deserialized and so contained only null references. I finally used IDeserializationCallback
to execute code after the object graph had been completely deserialized.
这篇关于自定义.NET序列化似乎并没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!