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 9245775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:20:25+00:00 2026-06-18T09:20:25+00:00

I was looking through some old code today and found an event handler that

  • 0

I was looking through some old code today and found an event handler that looked like this:

public void HandleEvent(EventClassA eventObj)
{
    if(eventObj is EventSubClassA)
    {
        HandleEventSubClassA(eventObj as EventSubClassA);
    }
    else if(eventObj is EventSubClassB)
    {
        HandleEventSubClassB(eventObj as EventSubClassB);
    }
    else if(eventObj.GetType() == typeof(EventSubClassC))
    {
        HandleEventSubClassC(eventObj as EventSubClassC);
    }
    else if(eventObj is EventSubClassD)
    {
        HandleEventSubClassD(eventObj as EventSubClassD);
    }
}

I thought this was kind of ugly. So I refactored it like this:

delegate void EventHandler(dynamic eventObj);
private static readonly Dictionary<Type, EventHandler> EVENT_MAP = new Dictionary<Type, EventHandler>()
    {
        { typeof(EventSubClassA), HandleEventSubClassA },
        { typeof(EventSubClassB), HandleEventSubClassB },
        { typeof(EventSubClassC), HandleEventSubClassC },
        { typeof(EventSubClassD), HandleEventSubClassD }
    };

public void HandleEvent(EventClassA eventObj)
{
    EVENT_MAP[eventObj.GetType()](eventObj);
}

private void HandleEventSubClassA(dynamic evt)
{
    var eventObj = evt as EventSubClassA;
}

I had a coworker review the code and there were concerns about the way this solution worked compared to the previous solution. I have a hard time believing that the previous solution is the best solution for this case, so I’ve turned to StackOverflow.

Is there a better way to build this type of class?
Is there a pattern I’m not aware of that is designed for this?

  • 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-06-18T09:20:26+00:00Added an answer on June 18, 2026 at 9:20 am

    You can use generics to make your existing solution slightly safer:

    private static Dictionary<Type, Delegate> handlers;
    
    static HandlerClass()
    {
        handlers = new Dictionary<Type, Delegate>();
        AddHandler<EventSubClassA>(HandleEventSubClassA);
        AddHandler<EventSubClassB>(HandleEventSubClassB);
        ...
    }
    
    public static void AddHandler<T>(Action<T> handler) where T : EventClassA
    {
        handlers[typeof(T)] = handler;
    }
    
    public void HandleEvent(EventClassA @event)
    {
        Delegate handler;
        if(handlers.TryGetValue(@event.GetType(), out handler))
        {
            handler.DynamicInvoke(@event);
        }
    }
    

    Alternatively, if you can modify the classes in your event hierarchy you could implement the visitor pattern:

    public interface IHandlers
    {
        void HandleSubClassA(EventSubClassA a);
        void HandleSubClassB(EventSubClassB b);
        ...
    }
    
    public abstract class EventClassA
    {
        public abstract void Visit(IHandlers handlers);
    }
    
    public class EventSubClassA : EventClassA
    {
        public override void Visit(IHandlers handlers)
        {
            handlers.HandleSubClassA(this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking through some old code and found a piece that i cant'
While looking through some old code I came across this gem: MyObject o =
So I'm looking through some code, and I see this: class whatever { public:
Looking through some old company code, I came across a for loop that looks
I'm looking through some code for learning purposes. I'm working through this portion of
I was looking through some compiled coffee-script code, and I noticed something like the
I have an application that is looking through some files for old data. In
When looking through some code that was handled by another employee, I see a
I'm looking through some old code and realize there are a ton of helper
I have been looking through some code on an open source project recently and

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.