动态调度到C#中的派生类 [英] Dynamic dispatch to derived class in C#

查看:47
本文介绍了动态调度到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命中了fallback方法,而不是派生类中的方法.我一直在同一个类中使用动态调度,并发现它真的非常有用/干净.如上例所示,它不适用于派生类吗?

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?

答案似乎有些混乱.基本上,我一直都在使用以下设计:

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
    { 

    }
}

非常适合以下情况:您事先不知道确切的类型并且不想使用大的switch语句.只是想知道是否可以通过继承实现该方法,其中基类拥有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.

推荐答案

它对您不起作用,因为即使evt是动态传递的,ProcessEvent也不会声明为虚拟.这意味着在编译对ProcessEvent的调用时,它将链接到在基类中找到的方法的唯一实现,并且派生类中的方法将永远不会执行.此外,您不能简单地将ProcessEvent声明为虚拟的,因为在派生类中签名会有所不同.

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.

为了使代码按预期工作,您可以在派生类中重写ReceiveEvent,使其完全相同:

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);
    }

如果要管理基类中的未处理事件,只需将基类中Process事件的修饰符更改为protected(否则,当重写版本的ReceiveEvents调用时,将无法执行该修饰符).

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).

这篇关于动态调度到C#中的派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆