Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5945369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:40:19+00:00 2026-05-22T16:40:19+00:00

Say I have the following code: public class SomeClass { // Other stuff… public

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:40:20+00:00Added an answer on May 22, 2026 at 4:40 pm

    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 T is unless there are constraints on it. The method call Handle(evt.Payload); is resolved once with only the knowledge that the type of evt.Payload is T, so it has to resolve to Handle<T>(T evt).

    If you really want overload resolution at execution time, and if you’re using C# 4, you can use dynamic typing:

    public void ApplyEvent<T>(IPublishedEvent<T> evt)
    {
        dynamic payload = evt.Payload;
        Handle(payload);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have the following code: public class Base { // Some stuff
Lets say that I have the following code: public class Shelter<A extends Animal, B
Say suppose I have the following Java code. public class Example { public static
Let's say we have the following piece of code: public class Event { }
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
Let's say I have the following code: public class Foo { private int x;
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
Assume I have the following code: public static class Foo { public static void
Let's say I have the following minimalistic HTML code: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
Say I have the following code: class Parent { static string MyField = ParentField;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.