Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic dispatch to derived class in C#

I'm trying to do the following:

public abstract BaseClass {

  public virtual void ReceiveEvent(Event evt)
    {
        ProcessEvent(evt as dynamic);
    }

    private void ProcessEvent(object evt)
    { 
        LogManager.Log(@"Received an event that is not being processed! 
                        Dispatch fallback");
    }
}

public DerivedClass: BaseClass {

    private void ProcessEvent(SpecificEvent evt)
    { 
        LogManager.Log("Processing Event");
    }
}

SpecificEvents hit the fallback method instead of the one in the derived class. I use dynamic dispatch within the same class all the time and find it really useful/clean. Will it not work with derived classes as illustrated in the example above?

EDIT: There seems to be some confusion in the answers. Basically i use the following design all the time:

public class SomeClass{

    public void DoSomethingDispatcher(SomeObject obj)
    {
        ProcessObject(obj as dynamic);
    }

    private void DoSomething(SomeObjectType1 obj)
    { 

    }

    private void DoSomething(SomeObjectType2 obj)
    { 

    }

    private void DoSomething(SomeObjectType3 obj)
    { 

    }

    private void DoSomething(object obj) //fallback
    { 

    }
}

Works great for when you don't know the exact type beforehand and you don't want to use a big switch statement. Just wondering if this can be implemented with inheritance where the base class holds the fallback method and the derived class holds all the more specific methods.

like image 392
Jamona Mican Avatar asked May 21 '12 10:05

Jamona Mican


People also ask

Does C have dynamic dispatch?

Unlike most native instruction sets, like x86, there are no arbitrary, un-typed jumps. But C, C++, and Rust all have some capability for dynamic dispatch: function pointers, virtual methods, and trait objects.

What is dynamic dispatch and how does it work in Objective C?

Dynamic dispatch. It simply means that the Objective-C runtime decides at runtime which implementation of a particular method or function it needs to invoke.

What is dynamic method dispatch explain with example?

In Java, Dynamic method dispatch is a technique in which object refers to superclass but at runtime, the object is constructed for subclass. In other words, it is a technique in which a superclass reference variable refers to a subclass object.

Is static dispatch faster than dynamic dispatch?

Static dispatch is typically faster than dynamic dispatch which by nature has higher overhead.


1 Answers

It's not working for you because even if evt is passed dynamic, ProcessEvent is not declared as virtual. This means that when the call to ProcessEvent is compiled, it is linked to the only implementation of the method that is found in the base class, and the ones in the derived classes will never be executed. Furthermore, you can't simply declare your ProcessEvent as virtual, since the signature will be different in the derived classes.

In order for your code to work as expected you could just override ReceiveEvent in the derived classes leaving it exactly the same:

  public override void ReceiveEvent(Event evt)
    {
        ProcessEvent(evt as dynamic);
    }

If you want to manage the unhandled events in the base class, just change the modifier of Process event in the base class to protected (otherwise it can't be executed when called by the overridden version of ReceiveEvents).

like image 50
Francesco Baruchelli Avatar answered Oct 06 '22 07:10

Francesco Baruchelli