根据我的问题(针对泛型方法的可重用非通用方法),我已经实现了提供的解决方案,但在进行一些重构(将代码移动到基类)后,我的代码会导致StackOverflowException,我不理解。
如果我将基类
调用Handle(new TestCommand())
会导致调用IMessageHandler.Handle(IMessage)
,然后调用Handle<TMessage>(TMessage)
,但它没有调用继承类的Handle(TestCommand message)
,而是再次调用IMessageHandler.Handle(IMessage)
。
通用方法中的as-cast到IMessageHandler<TestCommand>
有效,否则它会调用HandleUnknownMessage(IMessage)
。
很难描述,所以这里是我的测试代码:
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
中,代码可以正常工作。使用动态关键字与泛型方法和继承有什么限制吗?
IMessageHandler<T>
定义了一个Handle<T>(T)
方法吗?编辑:你能发布IMessageHandler
和IMessageHandler<T>
吗? - Chris SinclairProcessBase
上的私有Handle<TMessage>
方法,以避免方法解析时出现混乱) - Chris Sinclair