OK, I figured it out (I think)
[NonSerialized]
EventHandler _nonSerializableHandlers;
EventHandler _serializableHandlers;
/// <summary>
/// Declares a serialization-safe IsDirtyChanged event.
/// </summary>
public event EventHandler IsDirtyChanged
{
add
if (value.Target.GetType().IsSerializable)
_serializableHandlers =
(EventHandler)Delegate.Combine(_serializableHandlers, value);
else
_nonSerializableHandlers =
(EventHandler)Delegate.Combine(_nonSerializableHandlers, value);
}
remove
(EventHandler)Delegate.Remove(_serializableHandlers, value);
(EventHandler)Delegate.Remove(_nonSerializableHandlers, value);
/// Call this method to raise the IsDirtyChanged event.
virtual protected void OnIsDirtyChanged()
if (_nonSerializableHandlers != null)
_nonSerializableHandlers(this, EventArgs.Empty);
if (_serializableHandlers != null)
_serializableHandlers(this, EventArgs.Empty);
I temporarily forgot that C# makes you invoke the delegate directly anyway, so having a separate clause in the manual event declaration isn’t required. I still think it makes the code easier to read, but functionality is king in the end.
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Rockford Lhotka
E-mail