I attempt to serialize an object, and it throws an exception as shown below. I assume it attempts to serialize also the UserControl that subsribes to an event of the class I try to serialize. This is not desired. So how do I avoid serializing events? (as easy as possible)
SerializationException occured:
Type ‘System.Windows.Forms.UserControl’ in Assembly
‘System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089’ is not marked as serializable.
Public Sub SendData(ByVal obj As Object)
If Client.Connected Then
Try
Dim ns As NetworkStream = Client.GetStream()
Dim bf = New BinaryFormatter()
Dim ms = New MemoryStream()
bf.Serialize(ms, obj) ' < Exception here
The solution is to mark the event handlers in your class with [NonSerialized] attribute.