• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

.NET 2.0 Detecting deserialization

ASB

Godlike
Joined
Jul 6, 2005
Messages
5,480
Likes received
210
Thanks received
91
Location
Somerset
I have on object which a client wishes to serialize and deserialize - specifically via XML.

The problem is this means I have to ensure that the properties are read write. But I don't really want to do this since the data is "set once". So, how can I detect that an object is currently being deserialized by the XML deserializer.

Basically what I need to do isonly allow the property to be set during deserialization, IDeserializationCallback doesn't help since that only has OnDeserialization called after completion of deserialization.

I think OnDeserializingAttribute/OnDerserializedAttribute might to appropriate so that I can detect deserialization is in progress. Anybody tried this ??
 
Or perhaps implement the IDeserializationCallback interface on your bog standard Serializable class and prevent the class property set{} methods from setting the private member variables in IDeserializationCallback.OnDeserialization using a bool flag.
 
Or perhaps implement the IDeserializationCallback interface on your bog standard Serializable class and prevent the class property set{} methods from setting the private member variables in IDeserializationCallback.OnDeserialization using a bool flag.

Unfortunately that doesn't give me enough information. I need to to know it's actively being deserialized and IDesiralizationCallback only tells me it has been :tantrum:

My requirement is to allow them to be set when deserializing but not otherwise (except locally).

I could probably implement ISerializable but this seems a huge faff. [In an ideal world the 'set' would remain private. But this would mandate ISerializable. Write once properties are a right pain when it comes to serialization.
 
Surely you can set an internal bool once the serialization has been completed (IDesiralizationCallback) so that every public setter is then prevented from changing the data?

So during deserialization the flag _readOnly is false and the properties are set and once deserialization is complete the flag is set the true and every property setter is guarded?
 
Surely you can set an internal bool once the serialization has been completed (IDesiralizationCallback) so that every public setter is then prevented from changing the data?

So during deserialization the flag _readOnly is false and the properties are set and once deserialization is complete the flag is set the true and every property setter is guarded?

I thought that initially, but I don't think I can make it work.

In the case where the object is deserialized the contructor will be invoked, thus I can set _readonly to false, the properties set and the deserialization callback made then I can set _readonly to true.

However in the case where the object is created in my application the deserialization callback will not be made, thus I can't tell it is being actively deserialized. Granted I can get over this in a number of ways. It still leaves a situation where a client application can simply create an empty one and will have writable versions of the properites though.

I suppose I could still do something with CallContext though.

Overall it doesn't seem there is much of a solution, though I'll investigate Customer serialization with OnDeserializingAttribute/OnDerserializedAttribute. It lookas as though this might give me what I need.

/
 
Last edited: 11:07
Can you customise the serialization/deserialization code:

http://www.hanselman.com/blog/HOWTODebugIntoANETXmlSerializerGeneratedAssembly.aspx

Capture the code generated and then include it in your project. Modify to set the properties internally, set the actual properties to readonly. Use this code directly to serialize and deserialize.

Any good?

I think I'd probably use custome serialization in preference. It just "feels" as though I should have enough control to enable me to serilize and deserialize how I want to without having to do the faffing around. Obviously I accept "default serialization will do X". I just don't like the particular X they have chosen!
 
Surely you can set an internal bool once the serialization has been completed (IDesiralizationCallback) so that every public setter is then prevented from changing the data?

So during deserialization the flag _readOnly is false and the properties are set and once deserialization is complete the flag is set the true and every property setter is guarded?

That's exactly what I proposed.

I thought that initially, but I don't think I can make it work.

In the case where the object is deserialized the contructor will be invoked, thus I can set _readonly to false, the properties set and the deserialization callback made then I can set _readonly to true.

However in the case where the object is created in my application the deserialization callback will not be made, thus I can't tell it is being actively deserialized. Granted I can get over this in a number of ways. It still leaves a situation where a client application can simply create an empty one and will have writable versions of the properites though.

Can you reverse the logic? i.e. The prop setters are disabled by default and only enabled in the serialization constructor? It seems like this will work because if an empty class instance is created locally then all prop setters are auto-disabled for that instance, and they are only enabled when serialization is underway. It seems a simple solution but perhaps I'm missing something.
 

Advertisers

Back
Top