Say I have the following code:
public class SomeClass
{
// Other stuff...
public void ApplyEvent<T>(IPublishedEvent<T> evt)
{
Handle(evt.Payload);
}
protected virtual void Handle(ThingyCreatedEvent evt)
{
Code = evt.Code;
Label = evt.Label;
}
protected virtual void Handle<T>(T evt)
{
throw new NotImplementedException(
String.Format("Not set to handle {0}", evt.GetType().FullName));
}
}
evt.Payload is of type T. My hope was to be able to handle methods for specific event types I expect, and have a catch-all method that will throw if an unexpected event type is provided for some reason.
What I’ve found, though, is that the generic method gets called even if T is of type ThingyCreatedEvent, unless I explicitly cast it:
Handle(evt.Payload as ThingyCreatedEvent);
I get the same results if, instead of a generic Handle method, I define a Handle method with an Object type parameter.
Can anyone explain this? I’d like to have a better understanding of what’s going on here. I would have expected it to dispatch according to the actual type provided at run-time.
All the code – including overload resolution – is compiled once, at normal compilation time, and the overload decisions have to be made without any knowledge of what
Tis unless there are constraints on it. The method callHandle(evt.Payload);is resolved once with only the knowledge that the type ofevt.PayloadisT, so it has to resolve toHandle<T>(T evt).If you really want overload resolution at execution time, and if you’re using C# 4, you can use dynamic typing: