Friday, August 30, 2019
Store and Recover Information About Object Using Binary Serialization in C#
Report Store and Recover Information about Object Using Binary Serialization in C# What is Binary Serialization: Serialization is the process taking an object and converting it to a format which can be transported through net work or store into a storage medium, the storage medium could be file, database, or memory. The . NET Framework provides two types of serialization, XML serialization and Binary serialization. There are also three formats provided by the Microsoft . NET framework to which objects can be serialized. The formats are binary, SOAP, and XML.Binary serialization can either the binary or the SOAP formatter, are suitable for storing object Information in . NET applications, and you donââ¬â¢t need share the information with non-. NET programs. When I mention Binary serialization below I mean ââ¬Å"Binary serializationâ⬠using binary formatter, as short expression. Why use Binary Serialization: The binary serialization working chart is: [pic] The chart also can p resent the other two format of serialization. so why we use Binary serialization instead of use the other two? What the advantage and disadvantage of binary serialization?The binary serialization is the most compact and light of the three formats and it is the fastest one of the three formats. Also the binary serialization can serialize all the state of the object, including type information. Therefore when the object is deserialized, you get an accurate and fully functional copy of the original, which xml will not It ignores private member fields and properties. The main limitation of using binary serialization is that binary serialization depends on platform, while XML and SOAP do not adhere to that limitation.Therefore only when all your applications which use serialization are . net framework applications, you can use binary serialization, otherwise uses other two formats instead. Using Binary Serialization in C#: The code to using Binary serialize is very simple. The step is: 1 ) create an instance of an BinaryFormatter class (using the interface iForamatter) 2) pass it an object and an open stream 3) iFormatter. Serialize methodwrites the object's state to the stream. Below I will show a very simple code to serialize an object call aPerson to a file.To make a class serializable we must mark it with the serializable attribute at before the class code The code is: [Serializable] public class Person then we need 2 namespaces for using serialization in our application: using System. Runtime. Serialization. Formatters. Binary; using System. Runtime. Serialization; then use the code below to Serialize: //create an instance of an BinaryFormatter class(IFormatter is a interface) IFormatter formatter = new BinaryFormatter(); //create a stream Stream stream = new FileStream(ââ¬Å"filenameâ⬠, FileMode. Create, FileAccess.Write, FileShare. None); //pass stream and object to the stream formatter. Serialize method for doing Serialization formatter. Serialize(stre am, aPerson); //finished close the stream stream. Close(); The deserialize just as simple as serialize, the code are: IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(ââ¬Å"MyFile. datâ⬠, FileMode. Open, FileAccess. Read, FileShare. Read); Person clone = (Person) formatter. Deserialize(stream); stream. Close(); Some Important not for Serialization: ? the constructors are not called when an object is deserialized. the Serializable attribute cannot be inherited. ? there are some Security issues of serialization you can visit The Security and Serialization (http://msdn. microsoft. com/library/? url=/library/en-us/cpguide/html/cpconsecurityserialization. asp)topic in the Framework Documentation further reading: This report just explains the very basic idea of Binary serialization for further study the following links will help. Serializing Objects: http://msdn. microsoft. com/library/default. asp? url=/library/en-us/cpguide/html/cpovrSerializingObjects. sp C# Object Serialization by Budi Kurniawan http://www. ondotnet. com/pub/a/dotnet/2002/08/26/serialization. html References: Serializing Objects: http://msdn. microsoft. com/library/default. asp? url=/library/en-us/cpguide/html/cpovrSerializingObjects. asp http://www. c-sharpcorner. com/Language/serializingObjectsinCS. asp ââ¬âââ¬âââ¬âââ¬âââ¬âââ¬âââ¬âââ¬â Deserialization Binary Serialization network Deserialization Binary Serialization Copy of Your Object A File, Database or memory Storage medium Another Application Your object Your Application
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.