In C#, when you define an event in your class, e.g.:

image

the event han­dlers will be seri­al­ized along with other prop­er­ties, etc.

This is because under the hood, the com­piler trans­lates your event into the fol­low­ing, as can be seen through Jet­Brain’s dot­Peek decompiler:

image

Since the gen­er­ated Even­tHandler is not marked with the [Non­Se­ri­al­ized] attribute there’s no way for the seri­al­izer to know that it should be ignored dur­ing the seri­al­iza­tion process.

Solu­tion 1 : imple­ment event explicitly

One solu­tion to this is to imple­ment the event explic­itly, so you end up with some­thing along the line of:

image

Solu­tion 2 : use [field:NonSerialized]

A much bet­ter solu­tion would be to use the [Non­Se­ri­al­ized] attribute on the event, and more specif­i­cally, you want to apply it to the pri­vate Even­tHandler instance that the com­piler gen­er­ates for you. Apply­ing the [Non­Se­ri­al­ized] attribute on the event gen­er­ates a com­piler error telling you that it’s only valid on field declarations:

image

image

So instead, if you pre­fix the attribute with the ‘field’ key­word every­thing will work as you’d expect:

image

Clos­ing thoughts…

On a related note, you may have noticed that my event dec­la­ra­tion assigns an empty han­dler to the event, this is to remove the need for null check­ing before invok­ing an event (which as you can imag­ine requires even more work to make thread-safe). There are other tech­niques (see @BlkRabbitCoder’s post on rais­ing events safely and effi­ciently here for more details) to deal with this com­mon problem.

In the case of a class that has to be seri­al­ized, this tech­nique won’t work if the event han­dlers are not seri­al­ized as part of the object, because by the time it’s dese­ri­al­ized the event han­dler will be null.

So instead, cour­tesy of @BlkRabbitCoder, here’re two sim­ple exten­sion meth­ods that encap­su­lates the code nec­es­sary to safely (pre­vents Null­Ref­er­ence­Ex­cep­tion) invoke event han­dlers in a thread-safe manner:

image

Fur­ther reading

Here are some more blog posts that go in to more detail about some intri­ca­cies with events in .Net:

Jon Skeet – Del­e­gates and Events

Eric Lip­pert – Events and Races

Safely and Effi­ciently rais­ing Events

Share

Leave a Reply