Creating a Custom Event Dictionary






1.70/5 (9 votes)
An article on creating a custom event dictionary.
Introduction
This collection allows the developer to call any number of events from a centralized source.
Using the code
Simply instantiate the EventDictionary
class as you would a regular Dictionary
class, and pass in an EventHandler
using the EventDictionary.Add
method.
using System;
using System.Collections.Generic;
/// <summary>
/// Represents a collection of key/event pairs that are accessible by the
/// event name.
/// </summary>
public class EventDictionary : Dictionary<string, EventHandler>
{
...
/// <summary>
/// Adds an entry with the specified value into the EventDictionary
/// collection with the EventHandler's name as the key.
/// </summary>
/// <param name="e">The value of the event to add. This value cannot
/// be null (Nothing in Visual Basic).</param>
public void Add(EventHandler e)
{
if (e == null)
throw new ArgumentException("Event Handler cannot be null.",
"e");
string s = e.Target.GetType().GetEvents()[0].ToString();
base.Add(s.Substring(s.LastIndexOf(" ") + 1), e);
}
}