Information Technology Reference
In-Depth Information
Handlers.Add(system, ev);
}
static public void
RemoveLogger(
string
system,
EventHandler
<
LoggerEventArgs
> ev)
{
// will throw exception if system
// does not contain a handler.
Handlers[system] -= ev;
}
static public void
AddMsg(
string
system,
int
priority,
string
msg)
{
if
(
string
.IsNullOrEmpty(system))
{
EventHandler
<
LoggerEventArgs
> l =
null
;
Handlers.TryGetValue(system,
out
l);
LoggerEventArgs
args =
new
LoggerEventArgs
(
priority, msg);
if
(l !=
null
)
l(
null
, args);
// The empty string means receive all messages:
l = Handlers[
""
]
as
EventHandler
<
LoggerEventArgs
>;
if
(l !=
null
)
l(
null
, args);
}
}
}
The generic version trades casts and type conversions for increased code to
handle event maps. I'd prefer the generic version, but it's a close tradeoff.
Events provide a standard syntax for notifying listeners. The .NET Event
Pattern follows the event syntax to implement the Observer Pattern. Any
number of clients can attach handlers to the events and process them.
Those clients need not be known at compile time. Events don't need sub-
scribers for the system to function properly. Using events in C# decouples