31 May 2004
A few weeks ago I posted an entry about a solution to today's problem with serializing objects that declare events. It was pointed out that there's a better way to handle the list of delegates, and so here's a better version of the code. <nonserialized()>_Private mNonSerializable As EventHandler
Private mSerializable As EventHandler</nonserialized()> Public Custom Event NameChanged As System.ComponentModel.EventHandler
AddHandler(ByVal value As System.ComponentModel.EventHandler)
If value.Target.GetType.IsSerializable Then
mSerializable = CType([Delegate].Combine(mSerializable, value), EventHandler) Else
mNonSerializable = CType([Delegate].Combine(mNonSerializable, value), EventHandler)
End If
End AddHandler RemoveHandler(ByVal value As System.ComponentModel.EventHandler)
If value.Target.GetType.IsSerializable Then
mSerializable = CType([Delegate].Remove(mSerializable, value), EventHandler) Else
mNonSerializable = CType([Delegate].Remove(mNonSerializable, value), EventHandler)
End If
End RemoveHandler RaiseEvent(ByVal sender As Object, ByVal e As System.ComponentModel.EventArgs)
If mNonSerializable IsNot Nothing Then mNonSerializable(sender, e)
If mSerializable IsNot Nothing Then mSerializable(sender, e)
End RaiseEvent
End Event