Information Technology Reference
In-Depth Information
// The empty string means receive all messages:
l = Handlers[ "" ] as
EventHandler < LoggerEventArgs >;
if (l != null )
l( null , args);
}
}
}
This new example stores the individual event handlers in the
EventHandlerList collection. Sadly, there is no generic version of
EventHandlerList. Therefore, you'll see a lot more casts and conversions in
this block of code than you'll see in many of the samples in this topic.
Client code attaches to a specific subsystem, and a new event object is cre-
ated. Subsequent requests for the same subsystem retrieve the same event
object. If you develop a class that contains a large number of events in its
interface, you should consider using this collection of event handlers. You
create event members when clients attach to the event handler on their
choice. Inside the .NET Framework, the System.Windows.Forms.Control
class uses a more complicated variation of this implementation to hide the
complexity of all its event fields. Each event field internally accesses a col-
lection of objects to add and remove the particular handlers. You can find
more information that shows this idiom in the C# language specification.
The EventHandlerList class is one of the classes that have not been updated
with a new generic version. It's not hard to construct your own from the
Dictionary class:
public sealed class Logger
{
private static Dictionary < string ,
EventHandler < LoggerEventArgs >>
Handlers = new Dictionary < string ,
EventHandler < LoggerEventArgs >>();
static public void AddLogger(
string system, EventHandler < LoggerEventArgs > ev)
{
if (Handlers.ContainsKey(system))
Handlers[system] += ev;
else
 
Search WWH ::




Custom Search