在基类中使用dynamic关键字和调用泛型方法会导致StackOverflowException

时间:2012-12-18 12:07:46

标签: c# generics inheritance dynamic .net-4.0

根据我的question(通用方法的可重用非泛型方法)我实现了提供的解决方案,但经过一些重构(将代码移动到基类)后,我的代码导致了一个我不明白的StackOverflowException

Handle(new TestCommand())的调用会导致调用IMessageHandler.Handle(IMessage),然后调用Handle<TMessage>(TMessage),但不会调用继承类的Handle(TestCommand message),而是调用IMessageHandler.Handle(IMessage)再次。

泛型方法中的IMessageHandler<TestCommand>的投射会起作用,否则会调用HandleUnknownMessage(IMessage)

很难描述,所以这里是我的TestCode:

class Program {
    static void Main(string[] args) {

        MyProcess p = new MyProcess();
        IMessageHandler handler = p;

        handler.Handle(new DummyCommand()); // works -> HandleUnknownMessage gets called as expected

        handler.Handle(new TestCommand());  // fails -> results in a StackOverflowException 
    }
}  

public abstract class ProcessBase : IMessageHandler {
    void IMessageHandler.Handle(IMessage message) {
        System.Diagnostics.Debug.WriteLine("Dynamic Message gets handled");
        dynamic dynamicMessage = message;

        Handle(dynamicMessage);
    }

    private void Handle<TMessage>(TMessage message) where TMessage : IMessage {
        System.Diagnostics.Debug.WriteLine("Generic Message gets handled");
        var handler = this as IMessageHandler<TMessage>;
        if (handler == null)
            HandleUnknownMessage(message);
        else
            handler.Handle(message);
    }

    protected virtual void HandleUnknownMessage(IMessage unknownMessage) {
        System.Diagnostics.Debug.WriteLine("Unknown message {0} passed to Process".FormatWith(unknownMessage.GetType()));
        // Handle unknown message types here.
    }
}

public class MyProcess : ProcessBase, IMessageHandler<TestCommand>, IMessageHandler<TestEvent> {

    public void Handle(TestCommand commandMessage) {
        System.Diagnostics.Debug.WriteLine("TestCommand gets handled");
    }

    public void Handle(TestEvent eventMessage) {
        System.Diagnostics.Debug.WriteLine("TestEvent gets handled");
    }
}

public class DummyCommand : IMessage {    }

public class TestCommand : IMessage {    }

如果我将基类ProcessBase中的代码放到MyProcess,代码可以正常运行。动态关键字与泛型方法和继承的结合使用是否有任何限制?

1 个答案:

答案 0 :(得分:3)

尝试使用IMessageHandler<TMessage> void Handle<TMessage>(TMessage message)方法进行定义。

由于未定义,您定义的MyProcess.Handle事件仅属于 到该类,而不属于它实现的接口。当您的代码尝试调用IMessageHandler<TMessage> handler时,它不知道它实际上是具有这些特定方法的MyProcess。添加接口方法后,呼叫解决方案应识别MyProcess上存在这些方法并调用它们。否则它只会调用下一个最好的方法IMessageHandler.Handle(IMessage),它会产生无限循环。