Control object creation during deserialization

419 Views Asked by At

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.)

2

There are 2 best solutions below

0
On

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)

0
On

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