C# Raising events using reflection

Sat Oct 20 2012 03:19:00 GMT+0000 (Coordinated Universal Time)

/// <summary>
/// Raises the specified event by name.
/// </summary>
/// <param name="eventName">Name of the event.</param>
/// <param name="eventArgs">The <see cref="EventArgs" /> instance containing the event data.</param>
public void Raise(string eventName, EventArgs eventArgs)
{
  // Get the event field info
  var fieldInfo = this.GetType().GetField(eventName, BindingFlags.NonPublic | BindingFlags.Instance);
  // If the event exists
  if (fieldInfo != null)
  {
    var eventDelegate = (MulticastDelegate)fieldInfo.GetValue(this);
    // If there's any subscribed events...
    if (eventDelegate != null)
    {
      // Invoke their raise methods
      foreach (var handler in eventDelegate.GetInvocationList())
        handler.Method.Invoke(handler.Target, new object[] {this, eventArgs});
    }
  }
}
Who?
What?
My personal portfolio of things I come up with when I'm bored.