控制反序列化期间对象的创建
我想使用 NetDataContractSerializer(或我猜的任何序列化器)控制在反序列化期间通常不可序列化的类型的对象创建。使用自定义 SerializationBinder,我可以控制构造的类型,并使用自定义 ISurrogateSelector 和 ISerializationSurrogate 来控制如何在对象上设置状态。
我不能做的是实际上自己创建对象以允许使用依赖项注入或其他东西。导致问题的对象位于对象图内部,因此我无法在序列化之前对其进行编辑。
有没有办法允许我的代码构造反序列化对象?
(作为背景,我正在基于 WF 示例中的 XmlWorkflowInstanceStore 编写自定义 WF4 持久性实例存储。我想创作具有以下功能的工作流:作为接口的变量...并且无法直接构造具体类型。 XmlWorkflowInstanceStore 是我能找到的自定义持久性的唯一示例,它使用 NetDataContractSerializer 来序列化工作流状态。)
I would like to control object creation of a type that is not usually serializable during deserialization using a NetDataContractSerializer (or any serializer I guess). Using a custom SerializationBinder I can control the type that is constructed and using a custom ISurrogateSelector and ISerializationSurrogate to control how state is set on the object.
What I cannot do is actually create the object myself to allow the use of dependency injection or something. The object that is causing problems is inside the object graph so I cannot edit it before serialization.
Is there a way to allow my code to construct the deserialized object?
(For background, I am writing a custom WF4 persistence instance store based upon the XmlWorkflowInstanceStore in the WF samples. I want to author workflows that have variables that are interfaces... and the concrete types cannot be constructed directly. The XmlWorkflowInstanceStore is the only example I can find of custom persistence and it uses a NetDataContractSerializer to serialize the workflow state.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一直遇到这个问题。我通常自己实现 ISerialized 并使用一个方法设置所有依赖项(例如名为 SetDependency ) - 这并不是您真正想要的方式(因为您必须在反序列化后调用它),但我还没有找到更好的方法(尝试过 AOP 但结果很糟糕)
I run into this problem all the time. I normaly implement ISerializable myself and set all the dependencies with an method (named for example SetDependencies) - this is not truly the way you might want (because you have to call it after deserialization) but I didn't find a better way yet (tried AOP but it went to nasty)
我知道这个问题已经很老了,但是您在寻找 GetSafeUninitializedObject 吗?
I know this question is old, but were you looking for
GetSafeUninitializedObject
?