Object serialization with read-only members

  • Thread starter Thread starter Hans-Jürgen Philippi
  • Start date Start date
H

Hans-Jürgen Philippi

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
 
Hans-J?rgen Philippi said:
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.
 
Hans,

Assuming that the field internally is not read only (just the property
is), you could achieve what you want by implementing IXmlSerializable. 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.
 
Thomas,

Not really, you can always implement IXmlSerializable 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]
- (e-mail address removed)

Thomas T. Veldhouse said:
Hans-J?rgen Philippi said:
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.
 
Nicholas Paldino said:
Thomas,

Not really, you can always implement IXmlSerializable 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.
 
Hi Nicholas,
Assuming that the field internally is not read only (just the property
is), you could achieve what you want by implementing IXmlSerializable.
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 IXmlSerializable 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
 
Back
Top