Dynamically sending to a derived class in C #

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) //fallback { } } 

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.

+6
source share
4 answers

It does not work for you, because even if evt is passed dynamically, the ProcessEvent is not declared virtual. This means that when compiling a ProcessEvent call, it is associated with a single method implementation that resides in the base class, and those in the derived classes will never be executed. In addition, you cannot just declare your ProcessEvent as virtual, since the signature will differ in the derived classes.

In order for your code to work properly, you can simply override ReceiveEvent in derived classes, leaving it exactly the same:

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

If you want to manage unprocessed events in the base class, simply change the Process event modifier in the base class to the protected one (otherwise it cannot be executed when an overridden version of ReceiveEvents is called).

+4
source

If the method is not virtual / abstract in the base class, and the method is not marked as override in the derived class, it will never work.

Also, I don't understand the use of dynamic here.

+3
source

What is the type of your "evt" when it gets into a ProcessEvent?

You can take a look at Using a Dynamic Type :

A type is a static type, but an object of type dynamic bypasses is a static type check. In most cases, it functions as if it had an object type.

So evt is not a SpecificEvent .

+1
source

To get the expected behavior, you must override the virtual method:

 public DerivedClass: BaseClass { private override void ReceiveEvent(Event evt) { // Process your event here. } } 

With this code, ReceiveEvent will not be called in the base class, so the standby ProcessEvent will not be called.

There is no reason to use dynamic .

+1
source

Source: https://habr.com/ru/post/916240/


All Articles