0

Mono.Net on the Mac

Posted in .Net, Development, Uncategorized at February 16th, 2007 by Ben / No Comments »

Check it!

2

How to: prevent serialization of event handlers

Posted in .Net, C# at February 12th, 2007 by Gerrod / 2 Comments »

Today I hit a snag with Serialization, where-by the class that I was trying to serialize (into ViewState) had event handlers declared in it:

/// <summary>
/// Invoked after validation has completed.
/// </summary>
public event EventHandler ValidationComplete;

The problem was that I was subscribing to the event handler from a page’s code-behind, and I had absolutely no intention of marking the code-behind file as [Serializable]. Hence, I was getting an “Error serializing value” type of exception being thrown.

So, how do you stop this happening? Well, unfortunately, you can’t just slap a [NonSerialized] attribute in front of the event, because that attribute can only be used on fields, and technically an event isn’t a field. But actually, this is both the problem and the solution – you have to use a NonSerialized attribute with the “attribute location (set) to field”:

/// <summary>
/// Invoked after validation has completed.
/// </summary>
[field:NonSerialized] public event EventHandler ValidationComplete;

In all honesty I’ve never seen this type of modifier before, but this is taken straight from the MSDN doco for the attribute. It definitely works, but I’d love to know more about why, so if anyone has a clue, please enlighten me.

Adding quotes to html attributes with Find and Replace

Not the greatest regex engine but this is what I could manage. It still matches on all attributes even if they are quoted though. If you know how to improve please let me know.