views:

143

answers:

3

We wish to use the Binary Formatter. In debugging, thus far, it seems that it does not execute the getters for public properties. Does the XML Serializer behave the same way? Also, during deserialization, will the deserializers use the setters to apply the values during deserialization?

Thus far, our testing with BinaryFormatter shows that it simply writes directly to and from member variables. It does not step through any of the getters or setters. Is the XML Serializer the same way?

What if a public property did something silly like Random().Next? Will this be serialized by the Binary Formatter? It seems that with the XML Serializer, you would need to decorate this member appropriately to get it to participate. The Binary Formatter seems to only work, again, on member variables.

Thanks.

+2  A: 

You need both a getter and a setter or the property will not be serialized. The reason for this is that the serializer assumes it can't set the value so transporting it would be wasteful.

You can even have an empty setter and it will work.

BC
I would prefer throwing a "NotImplementedException" to leaving it empty. IMO that's less likely to result in a bug when someone consumes the class. But I understand that was just hyperbole for an example.
Joel Coehoorn
I wonder if the deserializer would propagate that exception? I'm not sure.
BC
I was also thinking of some non-deterministic read-only properties. I suppose it depends upon what you want in the serialized version of your object. For example, a property that always returns Date.Now() would be an odd beast in this case.
Pittsburgh DBA
A: 

Think about it this way:

Let's say you're deserializing a class with several serialized properties, and the setter for the last property has side effects that can alter the values of another property. The altered property no longer reflects your serialized data. Do you really want it to use that setter?

On the other hand, what if there is no backing store for a property? Perhaps it's a composite property allowing you to get and set values of all the others at once. Arguably this property shouldn't be serialized (or only this property should be serialized, depending on how things work), but there could be other examples. How does the formatter know where to assign the value for such a property?

So which is it? I had to look it up and couldn't quickly find an authoritative source, but it looks like the XmlSerializer does use getters and setters while the BinaryFormatter does not use getters or setters.

And that kind of makes sense. My first point showed that you don't really want to use getter/setters. My 2nd point showed that you may have to use them. The binary formatter can just take the exact in-memory representation of the object, so it skips the getter/setters. The XmlSerializer, which doesn't have this ability, has to use the other method.

You should probably set up a quick test project for yourself so you can see it in action.

Joel Coehoorn
That is my point exactly! I am trying to ensure that the serializer/deserializer does not care about setters, etc., because I very much WANT to eliminate such side effects (datbase, file, socket, etc).
Pittsburgh DBA
Sorry, I wasn't quite finished yet- I accidentally posted the answer part way through.
Joel Coehoorn
So, does this mean that the serializers NEVER run getters/setters for properties? I hope so. It would be optimal if the serializer simply copied member variables, because that is the state of the object. Is that the case?
Pittsburgh DBA
It means you could make a case for either option. I actually had to look up what really happens.
Joel Coehoorn
+1  A: 

I just ran a quick test using the XML Serializer. To answer your question: Yes, it does use the getters durning serialization and it does use the setters during deserialization.

EDIT

Found this in the docs:

This example uses a binary formatter to do the serialization. All you need to do is create an instance of the stream and the formatter you intend to use, and then call the Serialize method on the formatter. The stream and the object to serialize are provided as parameters to this call. Although it is not explicitly demonstrated in this example, all member variables of a class will be serialized—even variables marked as private. In this aspect, binary serialization differs from the XMLSerializer Class, which only serializes public fields. For information on excluding member variables from binary serialization, see Selective Serialization.

TGnat
Thank you. I suppose this means that the choice between the two is critical. In our case, this means that BinaryFormatter is king. We want the STATE of the object saved for reconstitution. The XML, not so nice for this purpose. I see its use, but if all you need is save/load, then Binary wins.
Pittsburgh DBA
Excellent reference from the docs. Thank you.
Pittsburgh DBA