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

  • Home
  • SEARCH
  • 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 6815499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:48:54+00:00 2026-05-26T20:48:54+00:00

I’m improving an existing messaging system for my project, which should accept a variable

  • 0

I’m improving an existing messaging system for my project, which should accept a variable number of parameters. Right now generics are used to pass arguments to the system. A lot of code repeats itself, so the question is – is it possible to merge all the versions of the class that take different number of parameters into a one single class?
The whole messaging system I take as a foundation for mine can be found here: CSharpMessenger

Code excerpts:

public delegate void Callback();
public delegate void Callback<T>(T arg1);
public delegate void Callback<T, U>(T arg1, U arg2);

Version with no parameters:

static public class Messenger
{
    private static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();

    static public void AddListener(string eventType, Callback handler)
    {
        if (!eventTable.ContainsKey(eventType))
        {
            eventTable.Add(eventType, null);
        }
        // Add the handler to the event.
        eventTable[eventType] = (Callback)eventTable[eventType] + handler;
    }

    static public void Invoke(string eventType)
    {
        Delegate d;
        if (eventTable.TryGetValue(eventType, out d))
        {
            Callback callback = (Callback) d;
            if (callback != null)
            {
                callback();
            }
        }
    }
}

Version with one parameter:

static public class Messenger<T>
{
    private static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();

    static public void AddListener(string eventType, Callback<T> handler)
    {
        if (!eventTable.ContainsKey(eventType))
        {
            eventTable.Add(eventType, null);
        }
        // Add the handler to the event.
        eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
    }

    static public void Invoke(string eventType, T arg1)
    {
        Delegate d;
        if (eventTable.TryGetValue(eventType, out d))
        {
            Callback<T> callback = (Callback<T>) d;
            if (callback != null)
            {
                callback(arg1);
            }
        }
    }
}

Version with two parameters:

static public class Messenger<T, U>
{
    private static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();

    static public void AddListener(string eventType, Callback<T, U> handler)
    {
        if (!eventTable.ContainsKey(eventType))
        {
            eventTable.Add(eventType, null);
        }
        // Add the handler to the event.
        eventTable[eventType] = (Callback<T, U>)eventTable[eventType] + handler;
    }

    static public void Invoke(string eventType, T arg1, U arg2)
    {
        Delegate d;
        if (eventTable.TryGetValue(eventType, out d))
        {
            Callback<T, U> callback = (Callback<T, U>) d;
            if (callback != null)
            {
                callback(arg1, arg2);
            }
        }
    }
}

As you can see most of the code repeats itself, Is it possible to create some general version of the same class, that will take a variable number of parameters and to avoid repeating the same code so many times?

Thanks.

  • 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-26T20:48:55+00:00Added an answer on May 26, 2026 at 8:48 pm

    Callback parameters are normally passed in a subclass of EventArgs. There’s already a simple generic delegate that abstracts this: EventHandler<TEventArgs> where TEventArgs : EventArgs. The variability in parameters can be added to subclasses of EventArgs.

    So, if you can restructure your code to use these classes, you can have just one version. Something like this:

    public static class Messenger<TEventArgs> where TEventArgs : EventArgs {
      private static Dictionary<string, EventHandler<TEventArgs>> eventTable = 
        new Dictionary<string, EventHandler<TEventArgs>>();
    
      public static void AddListener(string eventType, EventHandler<TEventArgs> handler) {
        if (eventTable.ContainsKey(eventType)) {
          eventTable[eventType] = eventTable[eventType] + handler;
        }
        else {
          eventTable.Add(eventType, handler);
        }
      }
    
      public static void Invoke(string eventType, TEventArgs args) {
        EventHandler<TEventArgs> d;
        if (eventTable.TryGetValue(eventType, out d)) {
          if (d != null) {
            d(args);
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.