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....
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
.Net Serialization (C#/VB.Net) Serialization can be defined as the process of converting the state of an object instance to a stream of data, so that it can be transported across the network or can be persisted in the storage location.
Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be stored and transferred. .
Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With