When implementing events, one can provide code for add
and remove
a event handler. However, events can be accessed in three ways:
MyEvent += Handler; // add accessor
MyEvent -= Handler; // remove accessor
MyEvent(this, EventArgs.Empty); // not supported by an accessor
Wouldn't it be obvious to have another accessor called invoke
that is responsible for that? My thoughts are:
class BaseClass
{
public virtual event EventHandler MyEvent { add; remove; protected invoke; }
}
class DerivedClass : BaseClass
{
public override event EventHandler MyEvent
{
invoke
{
// new code before event
base.MyEvent(this, ...);
// new code after event
}
}
}
I know of the old-style pattern, which is to implement an OnMyEvent(...)
method. But there two important drawback with this approach:
- Event code is scattered -> less organized code base
- You can't refactor the event easily (e.g., rename it)
Edit: Obviously the compiler team already designed for this feature (See GetRaiseMethod()
).