【发布时间】:2014-04-16 09:44:39
【问题描述】:
反序列化时是否有可能跳过序列化流的下一个“条目”?关于面向插件的架构,序列化对象图的不同部分可能会在另一个环境中成为未知类型(假设它们可以被安全地忽略)。尝试反序列化这些当然会失败。
abstract class Thing{}
class OneThing : Thing {} // <-- known in environment A & B
class SomeThing : Thing {} // <-- only known in environment A
...
var things = new List<Thing>();
...
things.Add( (OneThing)(formatter.Deserialize(stream)) );
things.Add( (SomeThing)(formatter.Deserialize(stream)) ); // <-- skip in B
things.Add( (OneThing)(formatter.Deserialize(stream)) );
如何使用二进制格式化程序来实现这一点?我是否必须计算长度并检索序列化条目的明确类型名称(例如作为字符串)并将其存储在条目本身之前,以便在反序列化时跳过它(通过增加流指针)?或者有没有更好的替代方案,对序列化表示的特定操作问题较少?
【问题讨论】:
-
BinarySerializationEngine?你的意思是BinaryFormatter? -
是的(感谢您的提示)。我把它改成了
BinaryFormatter。
标签: c# serialization plugins binary-serialization