Object serialization with read-only members

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans-Jürgen Philippi

    Object serialization with read-only members

    Hi Group,

    I've defined a class with an 'ID' property, which is a GUID that is created
    at the time of the very first object instance creation. This member is not
    included when I serialize an object instance to XML, since it is read-only.
    Actually, I want to preserve the ID value with serialization/deserialization
    processes but I do *not* want it to be writeable from code by a simple
    myObject.ID = ... assignment - how to achieve this?

    As far as I can see, I have to write my own serialization/deserialization
    routines for this purpose. Does anyone know a less laborious way to get what
    I want here, e.g. by a certain ["AttributeLabel "] for XML serialization that
    I don't know yet?

    Thanks for your help,
    greetings

    Hans


  • Thomas T. Veldhouse

    #2
    Re: Object serialization with read-only members

    "Hans-J?rgen Philippi" <HJPhilippi@aol .comwrote:
    Hi Group,
    >
    I've defined a class with an 'ID' property, which is a GUID that is created
    at the time of the very first object instance creation. This member is not
    included when I serialize an object instance to XML, since it is read-only.
    Actually, I want to preserve the ID value with serialization/deserialization
    processes but I do *not* want it to be writeable from code by a simple
    myObject.ID = ... assignment - how to achieve this?
    >
    As far as I can see, I have to write my own serialization/deserialization
    routines for this purpose. Does anyone know a less laborious way to get what
    I want here, e.g. by a certain ["AttributeLabel "] for XML serialization that
    I don't know yet?
    >
    There needs to be a "setter" on there because when the class is again
    deserialized, it has to set the value back to the object. XML serialization
    does not work the same as the native .NET serialization, as it goes against
    properties rather than instance variables. I find this rather annoying as
    well. In short, I believe you need to roll your own.

    --
    Thomas T. Veldhouse
    Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Object serialization with read-only members

      Hans,

      Assuming that the field internally is not read only (just the property
      is), you could achieve what you want by implementing IXmlSerializabl e. The
      XmlSerializer will call this interface to serialize and deserialize your
      class. Basically, you would output the id tag, and then set it internally
      when your instance is dehydrated.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Hans-Jürgen Philippi" <HJPhilippi@aol .comwrote in message
      news:ukBqw6$qGH A.1368@TK2MSFTN GP05.phx.gbl...
      Hi Group,
      >
      I've defined a class with an 'ID' property, which is a GUID that is
      created at the time of the very first object instance creation. This
      member is not included when I serialize an object instance to XML, since
      it is read-only.
      Actually, I want to preserve the ID value with
      serialization/deserialization processes but I do *not* want it to be
      writeable from code by a simple myObject.ID = ... assignment - how to
      achieve this?
      >
      As far as I can see, I have to write my own serialization/deserialization
      routines for this purpose. Does anyone know a less laborious way to get
      what I want here, e.g. by a certain ["AttributeLabel "] for XML
      serialization that I don't know yet?
      >
      Thanks for your help,
      greetings
      >
      Hans
      >
      >

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Object serialization with read-only members

        Thomas,

        Not really, you can always implement IXmlSerializabl e and then you can
        put any values you want in the output stream, as well as read them back in
        when you rehydrate your object.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Thomas T. Veldhouse" <veldy71@yahoo. comwrote in message
        news:3P6dnSzg77-JByLZnZ2dnUVZ_t WdnZ2d@giganews .com...
        "Hans-J?rgen Philippi" <HJPhilippi@aol .comwrote:
        >Hi Group,
        >>
        >I've defined a class with an 'ID' property, which is a GUID that is
        >created
        >at the time of the very first object instance creation. This member is
        >not
        >included when I serialize an object instance to XML, since it is
        >read-only.
        >Actually, I want to preserve the ID value with
        >serializatio n/deserialization
        >processes but I do *not* want it to be writeable from code by a simple
        >myObject.ID = ... assignment - how to achieve this?
        >>
        >As far as I can see, I have to write my own serialization/deserialization
        >routines for this purpose. Does anyone know a less laborious way to get
        >what
        >I want here, e.g. by a certain ["AttributeLabel "] for XML serialization
        >that
        >I don't know yet?
        >>
        >
        There needs to be a "setter" on there because when the class is again
        deserialized, it has to set the value back to the object. XML
        serialization
        does not work the same as the native .NET serialization, as it goes
        against
        properties rather than instance variables. I find this rather annoying as
        well. In short, I believe you need to roll your own.
        >
        --
        Thomas T. Veldhouse
        Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
        >

        Comment

        • Thomas T. Veldhouse

          #5
          Re: Object serialization with read-only members

          Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
          Thomas,
          >
          Not really, you can always implement IXmlSerializabl e and then you can
          put any values you want in the output stream, as well as read them back in
          when you rehydrate your object.
          >
          Oh, yes indeed. I am not sure why that didn't occur to me. One thing I can
          say is that it is very tedious to do that [and ISerializable] if you have a
          large object graph.

          --
          Thomas T. Veldhouse
          Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

          Comment

          • Hans-Jürgen Philippi

            #6
            Re: Object serialization with read-only members

            Hi Nicholas,
            Assuming that the field internally is not read only (just the property
            is), you could achieve what you want by implementing IXmlSerializabl e.
            The XmlSerializer will call this interface to serialize and deserialize
            your class. Basically, you would output the id tag, and then set it
            internally when your instance is dehydrated.
            Yes, you're right, the private ID string is writeable without having a set
            {...} part in its related public property code.
            The point is: If I implement the IXmlSerializabl e interface, I end up
            writing the complete serialization/deserialization code on my own, right?
            And this is what I didn't want in my large class with a lot of members...

            Maybe there's a way to interact with the intrinsic XML serialization for a
            certain field only? That's what I was looking for.

            Thanks anyway,
            Hans


            Comment

            Working...