Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Overriding the Assembly and/or Type

When Deserializing an Object.................................................................. 640

 

Serialization is the process of converting an object or a graph of connected objects into a stream of bytes. Deserialization is the process of converting a stream of bytes back into its graph of connected objects. The ability to convert objects to and from a byte stream is an incredibly useful mechanism. Here are some examples:

■ An application’s state (object graph) can easily be saved in a disk file or database and then restored the next time the application is run. ASP.NET saves and restores session state by way of serialization and deserialization.

■ A set of objects can easily be copied to the system’s clipboard and then pasted into the same or another application. In fact, Windows Forms and Windows Presentation Foundation (WPF) use this.

■ A set of objects can be cloned and set aside as a “backup” while a user manipulates the “main”

set of objects.

 

■ A set of objects can easily be sent over the network to a process running on another machine. The Microsoft .NET Framework’s remoting architecture serializes and deserializes objects that are marshaled by value. It is also used to send objects across AppDomain boundaries, as dis- cussed in Chapter 22, “CLR Hosting and AppDomains.”


In addition to the preceding examples, after you have serialized objects in a byte stream in memory, it is quite easy to process the data in more useful ways, such as encrypting and compressing the data.

With serialization being so useful, it is no wonder that many programmers have spent count- less hours developing code to perform these types of actions. Historically, this code is difficult to write and is extremely tedious and error-prone. Some of the difficult issues that developers need to

grapple with are communication protocols, client/server data type mismatches (such as little-endian/ big-endian issues), error handling, objects that refer to other objects, in and out parameters, arrays of structures, and the list goes on.

Well, you’ll be happy to know that the .NET Framework has fantastic support for serialization and deserialization built right into it. This means that all of the difficult issues just mentioned are now handled completely and transparently by the .NET Framework. As a developer, you can work with your objects before serialization and after deserialization and have the .NET Framework handle the stuff in the middle.

In this chapter, I explain how the .NET Framework exposes its serialization and deserialization ser- vices. For almost all data types, the default behavior of these services will be sufficient, meaning that it is almost no work for you to make your own types serializable. However, there is a small minority of types where the serialization service’s default behavior will not be sufficient. Fortunately, the serial- ization services are very extensible, and I will also explain how to tap into these extensibility mecha- nisms, allowing you to do some pretty powerful things when serializing or deserializing objects. For example, I’ll demonstrate how to serialize Version 1 of an object out to a disk file and then deserialize it a year later into an object of Version 2.



       
   
 
 



Date: 2016-03-03; view: 727


<== previous page | next page ==>
Nbsp;   Using Reflection to Discover a Type’s Members | Nbsp;   Serialization/Deserialization Quick Start
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.006 sec.)