0
00unknown.user00
I'm trying to figure out if there is any way to determine if a class
has already registered as a handler for an event. Let's say I've got a
class Emitter that has an event
EmitterEvent
and a delegate
void EmitterEventHandler(Emitter sender)
I've got another class Listener that wants to listen for the
EmitterEvent. So I do the following:
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);
This, of course works, and if I do it twice:
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);
I recieve the event twice. I want to avoid that, so before registering
the EmitterEventHandler, I'd like to check if I'm already registered
for it. Something like this:
emitter.EmitterEvent.EmitterEvent.GetInvocationList().Contains(new
EmitterEventHandler(emitter_EmitterEvent);
Which is not possible.
Is there any way to do this? My current solution is to make the
EmitterEvents private and the following methods to my Emitter class:
public void RegisterHandler(EmitterEventHandler h)
{
//if EmitterEvent is null, just attach the Handler
if(this.EmitterEvent==null)
{
this.EmitterEvent+=h;
}
else
{
bool contains = false;
Delegate[] dels = EmitterEvent.GetInvocationList();
foreach(Delegate d in dels)
{
if(d == h)
{
contains = true;
break;
}
}
if(!contains)
{
this.EmitterEvent+=h;
}
}
}
and
public void UnRegisterHandler(EmitterEventHandler h)
{
this.EmitterEvent-=h;
}
This works, but isn't that pretty. Any better solutions?
has already registered as a handler for an event. Let's say I've got a
class Emitter that has an event
EmitterEvent
and a delegate
void EmitterEventHandler(Emitter sender)
I've got another class Listener that wants to listen for the
EmitterEvent. So I do the following:
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);
This, of course works, and if I do it twice:
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);
I recieve the event twice. I want to avoid that, so before registering
the EmitterEventHandler, I'd like to check if I'm already registered
for it. Something like this:
emitter.EmitterEvent.EmitterEvent.GetInvocationList().Contains(new
EmitterEventHandler(emitter_EmitterEvent);
Which is not possible.
Is there any way to do this? My current solution is to make the
EmitterEvents private and the following methods to my Emitter class:
public void RegisterHandler(EmitterEventHandler h)
{
//if EmitterEvent is null, just attach the Handler
if(this.EmitterEvent==null)
{
this.EmitterEvent+=h;
}
else
{
bool contains = false;
Delegate[] dels = EmitterEvent.GetInvocationList();
foreach(Delegate d in dels)
{
if(d == h)
{
contains = true;
break;
}
}
if(!contains)
{
this.EmitterEvent+=h;
}
}
}
and
public void UnRegisterHandler(EmitterEventHandler h)
{
this.EmitterEvent-=h;
}
This works, but isn't that pretty. Any better solutions?