Click here to Skip to main content
12,953,230 members (49,382 online)
Click here to Skip to main content
Articles » Languages » VB.NET » HowTo » Downloads


41 bookmarked
Posted 16 Jan 2004

Serialize VB.NET objects without the events

, 26 Jan 2004
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()
    End Sub
    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)

        ' Instantiate base class

        ' 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)
            RaiseEvent ObjectAdded(Me, e)
        End Try
    End Sub

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

#End Region

#Region "Common Collection Methods"

    ' Simple collection implementation - you get the idea
    Public Sub Add(ByVal Obj As Object)
    End Sub

    Public Sub Remove(ByVal obj As Object)
    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.


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


About the Author

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

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 27 Jan 2004
Article Copyright 2004 by Trev Hunter
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid