In .NET (at least <=2) there's a problem serializing objects that raise events when those events are handled by a non-serializable object (like a Windows Form).
Because of the way VB.NET implements events, when you serialize an object, its events get serialized too (because events are actually implemented using hidden multicast delegate fields). A side effect of this is that any object which handles events raised by the object being serialized will be considered part of the object graph and will be serialized too.
Some workarounds could be found, implementing custom serialization or using delegates instead of events:
However none of them seems to completely satisfy the authors and users.
Does the .NET 3 / 4 solve this problem?
Serialization can be defined as the process of storing the state of an object to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, are converted to a stream of bytes, which is then written to a data stream.
Serialization is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database or ASP.NET Cache.
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization. Binary serialization is the process where you convert your . NET objects into byte stream.
Events are handled by creation of a delegate member. If you explicitly define this member yourself, you should be able to add the NonSerialized
attribute to it. See this thread for a reference.
For example:
Public Delegate Sub MyEventDelegate()
<NonSerialized()>Private m_MyEventDelegate As MyEventDelegate
Public Custom Event MyEvent As MyEventDelegate
AddHandler(ByVal value As MyEventDelegate)
m_MyEventDelegate = DirectCast(System.Delegate.Combine(m_MyEventDelegate, value), MyEventDelegate)
End AddHandler
RemoveHandler(ByVal value As MyEventDelegate)
m_MyEventDelegate = DirectCast(System.Delegate.Remove(m_MyEventDelegate, value), MyEventDelegate)
End RemoveHandler
RaiseEvent()
If m_MyEventDelegate IsNot Nothing Then
m_MyEventDelegate.Invoke()
End If
End RaiseEvent
End Event
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With