Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Serialization Works in .Net

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....

like image 964
Adam Driscoll Avatar asked Jan 21 '09 19:01

Adam Driscoll


People also ask

How does serialization work in C#?

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.

What is .NET serialization?

.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.

What is serialization in .NET with example?

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. .

How does serialize work?

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.


1 Answers

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.

like image 57
casperOne Avatar answered Sep 19 '22 23:09

casperOne