Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Serialization and Events : Page 2

Serialization process can result to problems when serializing objects that expose events. This is due to events handling in .NET. This paper describes two techniques to solve this problem, then it exposes some considerations about events and object lifecycle.


advertisement

Implementing ISerializable

The implementation of ISerializable is quite easy due to the power of reflection. The basic steps of the GetObjectData are the following

  1. Retrieve all events info
  2. Retrieve all fields info
  3. Iterate through the fields info and add the value to the SerializationInfo object if the field is not one of the EventNameEvent fields. This is done using the name of the field. The approach is correct as we saw that it is not possible to declare a field with the same name of a EventNameEvent.


Private Sub GetObjectData(ByVal info As SerializationInfo, _ ByVal context As StreamingContext) Implements Serializable.GetObjectData ' Serializes all public, private and public fields except the one ' which are the hidden fields for the eventhandlers ' Get the list of all events Dim EvtInfos() As EventInfo = Me.GetType.GetEvents() Dim EvtInfo As EventInfo ' Get the list of all fields Dim FldInfos() As FieldInfo = Me.GetType.GetFields( _ BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.Public) ' Loops in each field and decides wether to serialize it or not Dim FldInfo As FieldInfo For Each FldInfo In FldInfos ' Finds if the field is a eventhandler Dim Found As Boolean = False For Each EvtInfo In EvtInfos If EvtInfo.Name + "Event" = FldInfo.Name Then Found = True Exit For End If Next ' If field is not an eventhandler serializes it If Not Found Then info.AddValue(FldInfo.Name, FldInfo.GetValue(Me)) End If Next End Sub

The opposite is done in the special Sub New declared for deserialization

Private Sub New(ByVal info As SerializationInfo, _ ByVal context As StreamingContext) ' Get the list of the events Dim EvtInfos() As EventInfo = Me.GetType.GetEvents() Dim EvtInfo As EventInfo ' Get the list of the fields Dim FldInfos() As FieldInfo = Me.GetType.GetFields( _ BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.Public) ' Loops in each field and decideds wether to deserialize it or not Dim FldInfo As FieldInfo For Each FldInfo In FldInfos ' Finds if the field is a eventhandler Dim Found As Boolean = False For Each EvtInfo In EvtInfos If EvtInfo.Name + "Event" = FldInfo.Name Then Found = True Exit For End If Next ' If field is not a eventhandler deserializes it If Not Found Then FldInfo.SetValue(Me, info.GetValue(FldInfo.Name, FldInfo.FieldType)) End If Next End Sub

Using reflection makes it possible to build some standard procedures and to call them in the GetObjectData sub and in the sub new. This procedures could for example inspect if the field has the attribute < NonSerialized()>.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date