Serialization in dotNET
Well I never thought I would say this butttt..... serialization rocks man! I just discovered that it's quite easy to implement serialization with basic customization. All you need is to implement ISerializable in the base class.
For serialization, the method GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) needs to be implemented.
For deserialization, the constructor New(ByVal info as SerializationInfo, ByVal context as StreamingContext) needs to be implemented.
Any customization of the serialization process is to be done within these two methods. A note of caution though, when implementing own serialization process, it is entirely possible to enable serialization of classes without the SerializableAttribute attribute. For example:
Public Class FirstLevelClass Inherits BaseClass ...
It is possible to serialize FirstLevelClass even though it is NOT marked with the serializable attribute. As long as the highest level derived class is marked serializable, the serializer when called will attempt to serialize/deserialize.
Another requirement for own custom serialization is that each derived class MUST enable access to the constructor and the GetObjectData method. This depends on how the base class implements the method. If the GetObjectData is implemented in the base class as a public, then all derived classes do not need to reimplement it. However if the base class implements GetObjectData as protected, then each derived class needs to wrap it up and make a call to MyBase.GetObjectData(...).
The constructor on the other hand will always need to be implemented and wrapped around a call to MyBase.New(...).

0 Comments:
Post a Comment
<< Home