Serializable attribute and implement iserializable

Source: Internet
Author: User

SerializationIs the process of saving an object onto a storage medium (such as a file, or a memory buffer) or transmiting it processes ss a network connection link in binary form.This two ways is involved with binary serialization. have nothing to do with XML. (partition t for using soapformat which result in XML data .) for example. you can see the iserializable. getobjectdata is not called in XML serialization process. if you want to do that.

The. NET Framework provides an easy-to-use serialization mechanism by using the [serializable] attribute. If so why do we need the iserializable interface? Lets compare to see the differences...

1. the serializable attribute

Marking your class, struct, delegate or enum as [serializable] indicates that this class can be serialized. the. net runtime uses reflection at runtime, and by default all public and private fields are serialized, unless you apply thenonserializedattriction.

 
[Serializable]ClassMyclass {Public StringFirstname {Get; Set ;}}
2. The iserializable Interface

Implementing the iserializable interface allows the object to control the serialization process. We'll soon see what that means, but it's important to notice thatYou must also mark the class with the serializableattribute. Implementing the interface means, implementing two methods: getobjectdata to populate fields into the serializationinfo, and another constructor which is called during deserialization to restore object state fromSerializationinfo.

 [Serializable]  Class  Myclass: iserializable {  Public   String Firstname { Get ;Set  ;}  Public  Myclass (){}  Public   Void  Getobjectdata (serializationinfo info, streamingcontext context) {info. addvalue (  "  Firstname  "  , Firstname );// Will be called if you use soapformatter }  Protected Myclass (serializationinfo info, streamingcontext context) {firstname = Info. getstring ( "  Firstname  "  );}} 

 

Now lets try to compare them by a few aspects

Inheritance-In both ways, derived classes must be marked with the serializableattribute (for the 1st choice, that's all that needs to be done ). however, if you implement the interface, the derived class wocould probably need to implement it too (don't forget to also call the base class implementation ). another thing to remember is implementing the special constructor. it cannot be enforced by the compiler, So if you forget it-you'll get a runtime exception"The constructor to deserialize an object of type... Was not found."!

Control-Obviusly implementing the interface give you full control. however if all you want to do is to leave some fields out of the serialization, just use the nonserializedattribute attribute. if you need more control, like parameter ming some initialization code or validating the read values, use the iserializable interface.

Performance-Option 1 uses reflection. we know this is not the fastest way, however if your class contains only few primitive types the penalty is not so big. if you have more complicated collections you might consider serializing them yourself.

Versioning-If you add, remove or rename class fields, you will not be able to desirialize old saved object into a new one. To support this you will have to implement iserializable.

In summary, I am sure there are more aspects of serialization which are not covered here, but my conclusion and also Microsoft's advice is-use the serializableattribute unless your have a good reason to implement the iserializable interface.

See also:

Http://www.cnblogs.com/qqflying/archive/2008/01/13/1037262.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.