open associated files in the current Dovento exploit the mechanisms of internal serialization vb.net you can run into a problem. When the class has to be serialized events run by non-serializable object (eg a form window) serialization crashes.
Everything comes from the impossibility to declare serializable events. Following the instructions from
Using the extended event declaration can be used a double list of objects that handle events generated class, the objects serializzabili e quelli non serializzabili.
_
Private mNonSerializableHandlers As New Generic.List(Of EventHandler)
Private mSerializableHandlers As New Generic.List(Of EventHandler)
Public Custom Event NameChanged As EventHandler
AddHandler(ByVal value As EventHandler)
If value.Target.GetType.IsSerializable Then
mSerializableHandlers.Add(value)
Else
If mNonSerializableHandlers Is Nothing Then
mNonSerializableHandlers = _
New Generic.List(Of EventHandler)()
End If
mNonSerializableHandlers.Add(value)
End If
End
RemoveHandler AddHandler (ByVal value As EventHandler) If
value.Target.GetType.IsSerializable Then
mSerializableHandlers.Remove (value) Else
mNonSerializableHandlers.Remove (value) End If
RemoveHandler
End RaiseEvent (ByVal sender As Object, ByVal e As EventArgs) For Each item As EventHandler
In mNonSerializableHandlers
item.Invoke (sender, e)
Next For Each item As EventHandler In mSerializableHandlers
item.Invoke (sender, e) End RaiseEvent
Next End Event
In this way the problem was around, needless to say that the syntax is quite extensive, effort pays off serialization.
If there are events with parameters, you must weigh down even further, using a delegate:
_
Private mNonSerializableHandlers As New Generic.List (Of On_Progress)
Private mSerializableHandlers As New Generic.List (Of On_Progress) Public Delegate Sub
On_Progress (ByVal status As String, ByVal progress As Integer) Public Custom Event Progress As On_Progress
AddHandler (ByVal value As On_Progress) If value.Target.GetType.IsSerializable Then
mSerializableHandlers.Add(value)
Else
If mNonSerializableHandlers Is Nothing Then
mNonSerializableHandlers = New Generic.List(Of On_Progress)()
End If
mNonSerializableHandlers.Add(value)
End If
End AddHandler
RemoveHandler(ByVal value As On_Progress)
If value.Target.GetType.IsSerializable Then
mSerializableHandlers.Remove(value)
Else
mNonSerializableHandlers.Remove(value)
End If
End RemoveHandler
'
RaiseEvent(ByVal status As String, ByVal progress As Integer)
For Each item As On_Progress In mNonSerializableHandlers
item.Invoke(status, progress)
Next
For Each item As On_Progress In mSerializableHandlers
item.Invoke(status, progress)
Next
End RaiseEvent
End Event
In questo modo è possibile serializzare e deserializzare la nostra classe
'SERIALIZE
Dim FS As New System.IO.FileStream("c:\prova.wbp", FileMode.OpenOrCreate) As New BinaryFormatter Dim
BinFormatter
'
BinFormatter.Serialize (FS, myClassWithEvents)
' Deserialize Dim FS As New System.IO.FileStream
(file, FileMode.Open) Dim
BinFormatter As New BinaryFormatter
'RETURN
BinFormatter.Deserialize (FS)