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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:46:17+00:00 2026-06-13T10:46:17+00:00

I have a class that goes like this: public static class Messenger<T> { private

  • 0

I have a class that goes like this:

public static class Messenger<T>
{
    private static readonly Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
    public static void DoSomethingWithEventTable() //Somehow fills eventTable
    public static void Clear() 
    {
        eventTable.Clear();
    }
}

Now, I called DoSomethingWithEventTable two times somewhere in my program, like this:

Messenger<int>.DoSomethingWithEventTable();
Messenger<float>.DoSomethingWithEventTable();

I want to clear eventTable for every Messenger<T>. How should I do it? Should I call Clear for every type that I have put in generics, like this:

Messenger<int>.Clear();
Messenger<float>.Clear();

Or will it be enough to do something silly like this once:

Messenger<string>.Clear();

UPD: Basic experiments show that I should clear the Messenger for every used T. Now could somebody come with better design for the classes?

The more detailed version of what I am using now:

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

    static public void AddListener(string eventType, Callback<T> handler)
    {
        // Obtain a lock on the event table to keep this thread-safe.
        lock (eventTable)
        {
            // Create an entry for this event type if it doesn't already exist.
            if (!eventTable.ContainsKey(eventType))
            {
                eventTable.Add(eventType, null);
            }
            // Add the handler to the event.
            eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
        }
    }

    static public void RemoveListener(string eventType, Callback<T> handler)
    {
        // Obtain a lock on the event table to keep this thread-safe.
        lock (eventTable)
        {
            // Only take action if this event type exists.
            if (eventTable.ContainsKey(eventType))
            {
                // Remove the event handler from this event.
                eventTable[eventType] = (Callback<T>)eventTable[eventType] - handler;

                // If there's nothing left then remove the event type from the event table.
                if (eventTable[eventType] == null)
                {
                    eventTable.Remove(eventType);
                }
            }
        }
    }

    static public void Invoke(string eventType, T arg1)
    {
        Delegate d;
        // Invoke the delegate only if the event type is in the dictionary.
        if (eventTable.TryGetValue(eventType, out d))
        {
            // Take a local copy to prevent a race condition if another thread
            // were to unsubscribe from this event.
            Callback<T> callback = (Callback<T>)d;

            // Invoke the delegate if it's not null.
            if (callback != null)
            {
                callback(arg1);
            }
        }
    }

    static public void Clear()
    {
        eventTable.Clear();
    }
}

It is also important that I have another classes Messenger (non-generic, yeah) and Messenger<T,M>, and maybe someday I would even need something like Messenger<T,M,N>, etc.

  • 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-13T10:46:18+00:00Added an answer on June 13, 2026 at 10:46 am

    Each Messenger<T> type will have it’s own copy of eventTable so you will need to call Clear() for every different T you have used.

    As shown by this test:

    [TestFixture]
        public class Tests
        {    
            static class MyClass<T>
            {
                public static List<int> Member = new List<int>();
            }
    
            [Test]
            public void StaticTest()
            {
                var m1 = MyClass<int>.Member;
                var m2 = MyClass<string>.Member;
    
                Assert.AreNotSame(m1, m2);
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .net application (c#) that goes something like this public partial class
I have class that looks like this: class A { public: class variables_map vm
I've created a singleton which in the constructor goes like this: public static class
I have a Class that has a private variable with a public setter/getter function:
Let's say I have an interface, like this: public interface ILoggable { void Log(Func<string>
The story goes like this: I have an abstract class called Algorithms and a
I have an error that goes like this In file included from Level.hpp:12, from
I have a class model somewhat like this: public class MyModel { long MyModelID
I have an abstract base class called WidgetBase which looks like this: public abstract
I have some javascript that goes out and fetches a javascript class on another

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.