I have a feeling this is a repost but I can’t seem to find any good information about it. I was just wondering how serialization actually works (well actually deserialization). What I’m wondering is if say I have a property that is not actually backed by a private field; i.e.:
public string SomeProp { get { return GetValue('SomePropKey'); } set{ SetValue('SomePropKey', value); } }
When I deserialize does the setter get called? The getter gets called on serialization because when I serialize the object the correct value is written to the output stream. I know this seems like a strange circumstance, but what is really going on? Or am I just over complicating this….
It completely depends on the mechanism you are using for serialization.
If you are using XmlSerialization, then yes, the setter gets called.
If you are using data contract serialization (the DataContractSerialization) then the getter/setter will be called if you apply the DataMember attribute to the property (not to its backing field).
If you are using the original serialization mechanism in .NET (IFormatter implementation) then this scenario isn’t possible, because it will only serialize values stored in fields.