• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.

.NET 2.0 Detecting deserialization

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    .NET 2.0 Detecting deserialization

    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 ??

    #2
    Can you use the DataContractSerializer instead?

    http://msdn.microsoft.com/en-us/libr...erializer.aspx

    This can work with private fields which means you can make the public properties read-only using the DataMemberAttribute

    Comment


      #3
      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.
      Moving to Montana soon, gonna be a dental floss tycoon

      Comment


        #4
        Originally posted by DimPrawn View Post
        Can you use the DataContractSerializer instead?

        http://msdn.microsoft.com/en-us/libr...erializer.aspx

        This can work with private fields which means you can make the public properties read-only using the DataMemberAttribute
        Unfortunately not. Can't go to .NET 3.5. Have to stick with .NET 2.

        Comment


          #5
          Originally posted by TheRefactornator View Post
          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

          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.

          Comment


            #6
            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?

            Comment


              #7
              Originally posted by DimPrawn View Post
              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.

              /

              Comment


                #8
                Can you customise the serialization/deserialization code:

                http://www.hanselman.com/blog/HOWTOD...dAssembly.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?
                Last edited by DimPrawn; 11 June 2009, 10:12.

                Comment


                  #9
                  Originally posted by DimPrawn View Post
                  Can you customise the serialization/deserialization code:

                  http://www.hanselman.com/blog/HOWTOD...dAssembly.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!

                  Comment


                    #10
                    Originally posted by DimPrawn View Post
                    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.

                    Originally posted by ASB View Post
                    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.
                    Moving to Montana soon, gonna be a dental floss tycoon

                    Comment

                    Working...
                    X