Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Making a Type Serializable

When a type is designed, the developer must make the conscious decision as to whether or not to allow instances of the type to be serializable. By default, types are not serializable. For example, the following code does not perform as expected.

internal struct Point { public Int32 x, y; } private static void OptInSerialization() {

Point pt = new Point { x = 1, y = 2 }; using (var stream = new MemoryStream()) {

new BinaryFormatter().Serialize(stream, pt); // throws SerializationException

}

}


If you were to build and run this code in your program, you’d see that the formatter’s Serialize method throws a System.Runtime.Serialization.SerializationException exception. The problem is that the developer of the Point type has not explicitly indicated that Point objects may be serialized. To solve this problem, the developer must apply the System.SerializableAttribute custom attribute to this type as follows. (Note that this attribute is defined in the System namespace, not the System.Runtime.Serialization namespace.)

 

[Serializable]

internal struct Point { public Int32 x, y; }

 

Now, if we rebuild the application and run it, it does perform as expected and the Point objects will be serialized to the stream. When serializing an object graph, the formatter checks that every object’s type is serializable. If any object in the graph is not serializable, the formatter’s Serialize method throws the SerializationException exception.

       
   
 
 

 

The SerializableAttribute custom attribute may be applied to reference types (class), value types (struct), enumerated types (enum), and delegate types (delegate) only. (Note that enumerated and delegate types are always serializable, so there is no need to explicitly apply the SerializableAttribute attribute to these types.) In addition, the SerializableAttribute attribute is not inherited by derived types. So, given the following two type definitions, a Person object can be serialized, but an Employee object cannot.

 

[Serializable]

internal class Person { ... }

 

internal class Employee : Person { ... }


To fix this, you would just apply the SerializableAttribute attribute to the Employee type as well.

 

[Serializable]

internal class Person { ... }

 

[Serializable]

internal class Employee : Person { ... }

 

Note that this problem was easy to fix. However, the reverse—defining a type derived from a base type that doesn’t have the SerializableAttribute attribute applied to it—is not easy to fix. But, this is by design; if the base type doesn’t allow instances of its type to be serialized, its fields cannot be serialized, because a base object is effectively part of the derived object. This is why System.

Object has the SerializableAttribute attribute applied to it.

       
   
 
 

 

 


Date: 2016-03-03; view: 664


<== previous page | next page ==>
Nbsp;   Serialization/Deserialization Quick Start | Nbsp;   Controlling Serialization and Deserialization
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)