Inspired by Race the Sun
A 100% true-to-the-original Space Invaders clone
/// <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});
}
}
}