Saturday, May 18, 2019

Store and Recover Information About Object Using Binary Serialization in C#

Report Store and domesticise In coiffeion about Object Using binary program program serialisation in C What is binary program serialization Serialization is the process taking an object glass and converting it to a format which throne be transported through authorize work or store into a storage medium, the storage medium could be file, database, or memory. The . shed light on Framework provides two parts of serialisation, XML serialization and Binary serialization. There argon also three formats provided by the Microsoft . top framework to which objects can be serialized. The formats ar binary, slime, and XML.Binary serialization can either the binary or the SOAP formatter, are suitable for storing object Information in . NET applications, and you dont need percent the information with non-. NET programs. When I mention Binary serialization infra I mean Binary serialization using binary formatter, as short expression. Why use Binary Serialization The binary serializa tion working chart is pic The chart also can present the other two format of serialization. so why we use Binary serialization instead of use the other two? What the profit and disadvantage of binary serialization?The binary serialization is the most compact and light of the three formats and it is the hurried 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 hush-hush 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 B inary serialize is very simple. The step is 1) require an instance of an BinaryFormatter conformation (using the interface iForamatter) 2) pass it an object and an open drift 3) iFormatter. Serialize methodwrites the objects state to the stream. on a lower floor I will show a very simple code to serialize an object blazon out 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 burgeon forth 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(stream, aPerson) // terminate 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/? uniform resource locator=/library/en-us/cpguide/html/cpconsecurityserialization. asp)topic in the Framework bread and butter get ahead 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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.