Active1 year, 3 months ago
XML Serialization with the System.Xml.Serialization.XmlSerializer class. To get started, let’s create a simple class, add some attributes to help us control how properties are serialized, then we’ll serialize and deserialize the object. To get started, let’s create a C# Console Application that we will call SerializationSample.
I found out that some classes use the
[Serializable]
attribute.- What is it?
- When should I use it?
- What kinds of benefits will I get?
83922 gold badges1717 silver badges3131 bronze badges
kevinkevin5,7892424 gold badges7171 silver badges9898 bronze badges
6 Answers
What is it?
When you create an object in a .Net framework application, you don't need to think about how the data is stored in memory. Because the .Net Framework takes care of that for you. However, if you want to store the contents of an object to a file, send an object to another process or transmit it across the network, you do have to think about how the object is represented because you will need to convert to a different format. This conversion is called SERIALIZATION.
Uses for Serialization
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.
Apply
SerializableAttribute
to a type to indicate that instances of this type can be serialized. Apply the SerializableAttribute
even if the class also implements the ISerializable
interface to control the serialization process.All the public and private fields in a type that are marked by the
SerializableAttribute
are serialized by default, unless the type implements the ISerializable
interface to override the serialization process. The default serialization process excludes fields that are marked with NonSerializedAttribute
. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply NonSerializedAttribute
to that field.See MSDN for more details.
Edit 1
Any reason to not mark something as serializable
C Attribute Constructor
When transferring or saving data, you need to send or save only the required data. So there will be less transfer delays and storage issues. So you can opt out unnecessary chunk of data when serializing.
CharithJCharithJ38.2k1818 gold badges9595 silver badges114114 bronze badges
Some practical uses for the
[Serializable]
attribute:- Saving object state using binary serialisation; you can very easily 'save' entire object instances in your application to a file or network stream and then recreate them by deserialising - check out the
BinaryFormatter
class in System.Runtime.Serialization.Formatters.Binary - Writing classes whose object instances can be stored on the clipboard using
Clipboard.SetData()
- nonserialisable classes cannot be placed on the clipboard. - Writing classes which are compatible with .NET Remoting; generally, any class instance you pass between application domains (except those which extend from
MarshalByRefObject
) must be serialisable.
These are the most common usage cases that I have come across.
Bradley SmithBradley Smith11.1k33 gold badges3737 silver badges5252 bronze badges
Since the original question was about the SerializableAttribute, it should be noted that this attribute only applies when using the BinaryFormatter or SoapFormatter.
It is a bit confusing, unless you really pay attention to the details, as to when to use it and what its actual purpose is.
It has NOTHING to do with XML or JSON serialization.
Summary You can always count on Cnet. Did this blog post help in your issue? You assume responsibility for selecting the Software to achieve your intended results, and for the use and results obtained from the Software. After completing the file extraction, if the Self-Extractor window is still open, close it.
Used with the SerializableAttribute are the ISerializable Interface and SerializationInfo Class. These are also only used with the BinaryFormatter or SoapFormatter.
Unless you intend to serialize your class using Binary or Soap, do not bother marking your class as [Serializable]. XML and JSON serializers are not even aware of its existence.
BLaminackBLaminack
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.
How Serialization Works
This illustration shows the overall process of serialization.
The object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.
Mahbubur RahmanMahbubur Rahman3,15111 gold badge2121 silver badges3333 bronze badges
Here is short example of how serialization works. I was also learning about the same and I found two links useful.What Serialization is and how it can be done in .NET. Polldaddy hack 2017.
If you don't understand the above program a much simple program with explanation is given here.
Stacked3,97633 gold badges4646 silver badges6262 bronze badges
AkshayAkshay
43011 gold badge55 silver badges1313 bronze badges
Serialization
Serialization is the process of converting an object or a set of objects graph into a stream, it is a byte array in the case of binary serialization
Uses of Serialization
- To save the state of an object into a file, database etc. and use it latter.
- To send an object from one process to another (App Domain) on the same machine and also send it over wire to a process running on another machine.
- To create a clone of the original object as a backup while working on the main object.
- A set of objects can easily be copied to the system’s clipboard and then pasted into the same or another application
Below are some useful custom attributes that are used during serialization of an object
Have they changed the rules?Last post of this adobe support thread is implying all is not well post cancellationLove your work ?. I just want to access my catalog – i know develop should be greyed out. LR will only give me the trial/serial number and not all to play fair and let me sign in, it forces me to serial number page. Photoshop and lightroom package. Thanks for your prompt reply ? In AU/NZ price went from $9.99 to $11.99I can start the trial (although have a catalog issue!
[Serializable] -> It is used when we mark an object’s serializable[NonSerialized] -> It is used when we do not want to serialize an object’s field.[OnSerializing] -> It is used when we want to perform some action while serializing an object[OnSerialized] -> It is used when we want to perform some action after serialized an object into stream.
Below is the example of serialization
Here is the calling code
Sheo Dayal SinghSheo Dayal Singh