xmlserializer and value/reference types

  • Thread starter Thread starter Peter Webb
  • Start date Start date
P

Peter Webb

Following a suggestion in this newsgroup, I have used xmlserializer to
construct a save file. It works extremely well, and reduced a task that I
though would take a couple of days to less than an hour, learning time
included.

I did notice one strange thing though. It generated and loaded almost all of
my instance data, with the sole exception of the color fields. Looking at
the xml file, it generates a "stub" for color, but does not store the actual
color data. Indeed, the xml file consists entirely of value fields, with no
reference fields.

It took about 30 seconds to extract the RGB values and store them as value
fields in my class, and reconstitute the colors from these fields on load.

But it does seem strange that this wasn't done automatically for me - the
colors could have been saved in the xml as color.R, color.G, color.B and
rebuilt on load.

Why is this? Am I correct that xmlserializer will only work on value fields?
If so, why does this limitation exist? What am I missing?

Peter Webb
 
It doesn't relate to reference vs value types - and in fact, Color
*is* a value-type.
I don't know why it didn't serialize (although I have seen the
question before). An easy option would be to store the ARGB value as
an int, and use ToArgb and FromArgb to translate. Another option might
be to try using a serialization surrogate, but this is harder.
Finally, you could try DataContractSerializer - see if it behaves the
same.

Marc
 
As Marc says Color is a value type.

Reference types will serialize via XmlSerialize, in fact most of the demoes
on MSDN about XmlSerialize deal with classes (Reference types).

Maybe if you post your code we can see if there's something that's not being
done correctly.
 
Thanks to you both.

I looked at the C# list of value types - int, bool etc - and assumed they
were all there were. I didn't notice that many of the things I thought of as
..NET classes were actually structs. Doh. I had long wondered why things like
Color, Point, Rectangle etc were reference types. Now I know.

So I learned rather more than I expected.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top