I am 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 uses the return method, not the one found in the derived class. I use dynamic dispatching within the same class all the time and find it really useful / clean. Will it work with derived classes, as shown in the example above?
EDIT: There seems to be some confusion in the answers. I mainly 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)
Works great when you don't know the exact type in advance, and you don't want to use the large switch statement . It's just interesting if this can be implemented with inheritance, when the base class contains a return method and the derived class contains more and more specific methods.
source share