L
lorenzon
I've run into a problem in some code involving two class hierarchies
that I can't figure out: I have an event hierachy topped with an
interface, let's say-
IEvent,
EventA : IEvent,
EventB : IEvent
And some handlers-
abstract EventHandler
EventHandler1 : EventHandler
Now in the abstract EventHandler class I have a method which processes
events -
virtual void ProcessEvent(IEvent e) {}
and a loop which calls that method for every event I put on the
handler object (it runs in its own thread, but that's irrelevant). I
want the classes to process events like this:
class EventHandler1 : EventHandler
{
protected override void ProcessEvent(IEvent e)
{
//do some default processing in case there is no specific handler
}
protected void ProcessEvent(EventA e)
{
//specific processing for EventA type events
}
protected void ProcessEvent(EventB e)
{
//specific processing for EventB type events
}
}
So when the EventHandler method tries to process an event, it goes to
the ProcessEvent method for the most derived IEvent class for the
event object being passed in. What happens as it is is that the
ProcessEvent(IEvent) method is called, presumably because the event
object is boxed as an IEvent where it is held in the superclass.
I was kind of hoping that the runtime would just know what type the
event was, and call the appropriate method for that class. It doesn't
though, so is there a) some combination of language features such as
virtual / abstract / override that i can employ to achieve this, or b)
some way to dynamically unbox the event in the superclass? I looked
into dynamic casting and it doesn't seem to be possible in c#. But the
object knows what it is so it should be possible to extract it out of
its IEvent box and invoke an appropriate method through normal
polymorphism right?
that I can't figure out: I have an event hierachy topped with an
interface, let's say-
IEvent,
EventA : IEvent,
EventB : IEvent
And some handlers-
abstract EventHandler
EventHandler1 : EventHandler
Now in the abstract EventHandler class I have a method which processes
events -
virtual void ProcessEvent(IEvent e) {}
and a loop which calls that method for every event I put on the
handler object (it runs in its own thread, but that's irrelevant). I
want the classes to process events like this:
class EventHandler1 : EventHandler
{
protected override void ProcessEvent(IEvent e)
{
//do some default processing in case there is no specific handler
}
protected void ProcessEvent(EventA e)
{
//specific processing for EventA type events
}
protected void ProcessEvent(EventB e)
{
//specific processing for EventB type events
}
}
So when the EventHandler method tries to process an event, it goes to
the ProcessEvent method for the most derived IEvent class for the
event object being passed in. What happens as it is is that the
ProcessEvent(IEvent) method is called, presumably because the event
object is boxed as an IEvent where it is held in the superclass.
I was kind of hoping that the runtime would just know what type the
event was, and call the appropriate method for that class. It doesn't
though, so is there a) some combination of language features such as
virtual / abstract / override that i can employ to achieve this, or b)
some way to dynamically unbox the event in the superclass? I looked
into dynamic casting and it doesn't seem to be possible in c#. But the
object knows what it is so it should be possible to extract it out of
its IEvent box and invoke an appropriate method through normal
polymorphism right?