Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   How Formatters Serialize Type Instances

In this section, I give a bit more insight into how a formatter serializes an object’s fields. This knowl- edge can help you understand the more advanced serialization and deserialization techniques explained in the remainder of this chapter.

To make things easier for a formatter, the FCL offers a FormatterServices type in the System. Runtime.Serialization namespace. This type has only static methods in it, and no instances of the type may be instantiated. The following steps describe how a formatter automatically serializes an object whose type has the SerializableAttribute attribute applied to it.

1.The formatter calls FormatterServices’s GetSerializableMembers method.

 

public static MemberInfo[] GetSerializableMembers(Type type, StreamingContext context);

 

This method uses reflection to get the type’s public and private instance fields (excluding any fields marked with the NonSerializedAttribute attribute). The method returns an array of MemberInfo objects, one for each serializable instance field.

2.The object being serialized and the array of System.Reflection.MemberInfo objects are then passed to FormatterServices’ static GetObjectData method.

 

public static Object[] GetObjectData(Object obj, MemberInfo[] members);

 

This method returns an array of Objects where each element identifies the value of a field in the object being serialized. This Object array and the MemberInfo array are parallel. That is, element 0 in the Object array is the value of the member identified by element 0 in the MemberInfo array.

3.The formatter writes the assembly’s identity and the type’s full name to the stream.

4.The formatter then enumerates over the elements in the two arrays, writing each member’s name and value to the stream.


The following steps describe how a formatter automatically deserializes an object whose type has the SerializableAttribute attribute applied to it:

1.The formatter reads the assembly’s identity and full type name from the stream. If the as- sembly is not currently loaded into the AppDomain, it is loaded (as described earlier). If the assembly can’t be loaded, a SerializationException exception is thrown and the object cannot be deserialized. If the assembly is loaded, the formatter passes the assembly identity information and the type’s full name to FormatterServices’ static GetTypeFromAssembly method.

 

public static Type GetTypeFromAssembly(Assembly assem, String name);

 

This method returns a System.Type object indicating the type of object that is being deserialized.

2.The formatter calls FormatterServices’s static GetUninitializedObject method.

 

public static Object GetUninitializedObject(Type type);

 

This method allocates memory for a new object but does not call a constructor for the object. However, all the object’s bytes are initialized to null or 0.

3.The formatter now constructs and initializes a MemberInfo array as it did before by calling the FormatterServices’s GetSerializableMembers method. This method returns the set of fields that were serialized and that need to be deserialized.



4.The formatter creates and initializes an Object array from the data contained in the stream.

5.The reference to the newly allocated object, the MemberInfo array, and the parallel Object array of field values is passed to FormatterServices’ static PopulateObjectMembers method.

 

public static Object PopulateObjectMembers(

Object obj, MemberInfo[] members, Object[] data);

 

This method enumerates over the arrays, initializing each field to its corresponding value. At

this point, the object has been completely deserialized.

 

 


Date: 2016-03-03; view: 638


<== previous page | next page ==>
Nbsp;   Controlling Serialization and Deserialization | Nbsp;   Controlling the Serialized/Deserialized Data
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.009 sec.)