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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:09:19+00:00 2026-05-16T05:09:19+00:00

I have a set of messages, that all derive from Message, and implement the

  • 0

I have a set of messages, that all derive from Message, and implement the interface IMessage:

class Message : IMessage
{
   public string example1;
}

class LogOnMessage : Message
{
   public string password;
}

class LogOffMessage : Message
{
   public string anotherExample;
}

I have a messaging system, in which a proxy is able to detect incoming messages, and depending on the type of message, runs a delegate of the form

void MyDelegate(IMessage msg)

This is done by calling a handler registration function like this:

RegisterHandler(typeof(LogoffMessage),handler)

The implementation for this is simply a dictionary of the types, with each entry being a list of handlers: Dictionary< type, list< MyDelegate > >

Now, this is all fine, but of course the handler looks like this:

void MyHandler(IMessage msg)
{
   LogOffMessage lMsg = (LogOffMessage)msg;
   string athrEx = lMsg.anotherExample;

//Proceed with handling the logoff
}

Now, there’s a couple of issues with this. One is that you might try to convert the Msg into the wrong type.

The other is that it’s a bit inelegant to have to unwrap everything in the first few lines of any handler, rather than having handlers like this, which I’d like to have:

void MyDelegate(LogOffMessage msg)

What suggestions would you make?

I was thinking maybe instead of a dictionary of types, some other structure could be used to hold different delegate types, using generics. So there would be a MyDelegate < MsgType > list for each type. But it’s not clear how to make a collection of collections, where each inner collection type is different.

  • 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-16T05:09:20+00:00Added an answer on May 16, 2026 at 5:09 am

    Make your delegate generic:

    delegate void MessageHandler<T>(T message) where T : IMessage
    

    And make your RegisterHandler method generic:

    void RegisterHandler<T>(MessageHandler<T> handler)
    

    then call it with either:

    RegisterHandler(handler); // Let type inference handle it
    RegisterHandler<LogOffMessage>(handler); // State type argument explicitly
    

    Then when you fetch the delegate by type, you can cast it to the right handler type – even though it’s “unsafe” from the compiler’s point of view, you know that you’ll only have added the right kind of handler to the dictionary:

    // When you populate this, create the appropriate type of
    // `List<MessageHandler<T>>` based on the key
    private Dictionary<Type, object> handlerMap;
    
    void CallHandlers<T>(T message) where T : IMessage
    {
        object value;
        if (handlerMap.TryGetValue(typeof(T), out value))
        {
            List<MessageHandler<T>> handlers = (List<MessageHandler<T>>) value;
            foreach (MessageHandler<T> handler : handlers)
            {
                handler(message);
            }
        }
    }
    

    Note that another alternative to using a List<MessageHandler<T>> is just to combine delegates and end up with a multicast delegate in the map for each type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very uniform set of data from Radius messages that I need
I have a python script that analyzes a set of error messages and checks
I have a SQLite database that contains a huge set of log messages. I
I have a method that virtually deletes ALL messages of a current_user and by
I have to store messages that my web app fetch from Twitter into a
I have a class that implements IAuthorizationPolicy. I set up a custom Principal object
I have set up a Django application that uses images. I think I have
I have a main application that spawns a seperate thread to process messages off
I have two SSIS Solutions SolutionA Contains one utilities package that handles all emails
I want to send email messages that have arbitrary unicode bodies in a Python

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.