Archives

Archives / 2005 / April
  • VB.Net Event Implementation and Serialization Doesn’t Mix

    OK, another rant on why I can’t stand the way VB.Net does things differently from the way C# does them (and usually for some sort of backward compatibility to VB6).  This time the weirdness came by way of trying to using binary serialization on a class that listens to events on a class that isn’t marked serializable.  Recently another developer on my team add events to a class that is marked serializable, and then added listeners to the UI class by marking the class with the WithEvents attribute.  He then added event handlers by way of the handles attribute on the sub.  The problem is that since VB.Net uses hidden multicast delegate fields when you use this VB-centric method of handling events, the binary serializer will try to serialize the UI class listening in on the events.  Prior to adding this bit of code, binary serialization worked fine.  Now with the new code, binary serialization fails with an error saying that the UI class isn’t marked serializable.  So I had to search around trying to figure out what broke, and how to fix it.