Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

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. 

Turns out the fix is pretty easy.  Just don’t use the VB-centric style of event handling.  Use AddHandlers instead of WithEvents and the Handles clause, and you avoid some of the serialization issues.  Once I fixed it, then I could Google it and it looks like Trev Hunter ran into a similar issue, and wrote an article on it.  I haven’t checked this out in .Net 2.0 beta 2, but I hope they fixed this issue.

I’m pulling this blog post.  I put it together a little too quickly, and it is very hard to read, and some of the comments were just wrong.  That’s what I get for jumping to conclusions, and not doing my usual thing, and creating the simplest project possible to test the bug I found.  When I get some more time, I’ll post a sample app, with the issues I have (although they are not as bad as I first thought, there are differences between what you can do with VB.Net and C#).

Published Tuesday, April 26, 2005 10:14 PM by donxml
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Chip Lemmon said:

May 5, 2005 8:56 PM

Brad McCabe said:

May 5, 2005 8:59 PM

Paul Vick said:

Hey, Don, it'd be great if you could drop a comment by my blog explaining in greater detail what exactly happened and how you fixed it.

Your description above is a bit confusing. First, C# events introduce exactly the same multi-cast delegate fields that VB does when you declare an event (unless, of course, you declare the accessors yourself, something you can do in both languages in VS 2005). There is a small difference, which Trev calls out, in that VB doesn't have a way to propagate a NonSerializable attribute through to the underlying field (C# has a special [field:] target for attributes to accomplish this). But there is no fundamental difference in our code generation on this one.

Even more to the point, the fact that the UI class declared the variable holding the serializable class WithEvents should have no effect on the serializable class at all. Switching to AddHandler/RemoveHandler really shouldn't have any effect (except to just make your life a little harder than it needs to be).

If that did fix it, then something really strange must be going on, and we'd definitely love to understand what it is to make sure there isn't some undiagnosed bug you've found....

Paul
May 6, 2005 1:18 PM

Don Demsak said:

Paul, yes, I will update this post with code, and make it a bit easier to read (I sort of rush the blog entry, since I was in the middle of coding).

The short answer is that WithEvents forces the creation of the multi-cast delegate field at object construction, where the AddHandler, RemoveHandler methods allow you finer control, and you could execute RemoveHandler methods prior to serialization, and then execute AddHandler after serialization (thus avoiding the serialization issue).
May 6, 2005 2:35 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems