控制反序列化期间对象的创建

发布于 2024-09-17 11:34:01 字数 493 浏览 7 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-09-24 11:34:01

我一直遇到这个问题。我通常自己实现 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)

皓月长歌 2024-09-24 11:34:01

我知道这个问题已经很老了,但是您在寻找 GetSafeUninitializedObject 吗?

I know this question is old, but were you looking for GetSafeUninitializedObject?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文