Click here to Skip to main content
15,888,286 members
Articles / Programming Languages / Visual Basic

Serialize VB.NET objects without the events

Rate me:
Please Sign up or sign in to vote.
4.67/5 (19 votes)
26 Jan 20048 min read 270.5K   41  
Serialize VB.NET objects that have event handlers attached to its events.
Imports System.Runtime.Serialization
Imports System.Collections

'******************************************************************************
'*  NAME        :  SerializableCollectionWithEvents 
'*
'*  DESCRIPTION :   Test class to demonstrate the use of SerializableObject
'*                  when inheriting from a different base class
'*                  Of course, when serializing this collection, you have to 
'*                  ensure that objects stored in it are serializable too!
'******************************************************************************
<Serializable()> Public Class SerializableCollectionWithEvents
    Inherits CollectionBase
    Implements ISerializable, IDeserializationCallback

    ' Constructors
    Public Sub New()
        MyBase.New()
    End Sub
    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)

        ' Instantiate base class
        MyBase.New()

        ' Record the Info object between the constructor and the deserialization callback
        ' because if we set the values of fields that are declared with
        ' an initializer, the initializer will come along and overwrite the value
        ' after this constructor has been called
        ' This is for the same reason as SerializableObject itself
        mInfo = info

    End Sub

#Region "Events"

    Public Event ObjectAdded As EventHandler
    Protected Overridable Sub OnObjectAdded(ByVal e As EventArgs)
        Try
            RaiseEvent ObjectAdded(Me, e)
        Catch
        End Try
    End Sub

    Public Event ObjectRemoved As EventHandler
    Protected Overridable Sub OnObjectRemoved(ByVal e As EventArgs)
        Try
            RaiseEvent ObjectRemoved(Me, e)
        Catch
        End Try
    End Sub

#End Region

#Region "Common Collection Methods"


    ' Simple collection implementation - you get the idea
    Public Sub Add(ByVal Obj As Object)
        MyBase.InnerList.Add(Obj)
        Me.OnObjectAdded(EventArgs.Empty)
    End Sub

    Public Sub Remove(ByVal obj As Object)
        MyBase.InnerList.Remove(obj)
        Me.OnObjectRemoved(EventArgs.Empty)
    End Sub

#End Region

#Region "Serialization Implementation"

    ' Variable to store the serialization info between New() and OnDeserialization()
    <NonSerialized()> Private mInfo As SerializationInfo

    Protected Overridable Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData

        ' Use SerializableObject Shared method to do the serialization and filter out event delegates
        SerializableObject.SerializeObject(Me, info)

    End Sub

    Protected Overridable Sub OnDeserialization(ByVal sender As Object) Implements System.Runtime.Serialization.IDeserializationCallback.OnDeserialization

        ' Use SerializableObject Shared method to do the deserialization
        SerializableObject.DeserializeObject(Me, mInfo)

        ' Kill the info object
        mInfo = Nothing

    End Sub




#End Region


End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Currently a codemonkey that codes. Soon to be a fully-fledged monkey Wink | ;)

Comments and Discussions