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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:14:59+00:00 2026-05-31T00:14:59+00:00

I setup an event bus that simply passes a string for all event types.

  • 0

I setup an event bus that simply passes a string for all event types. This worked well but now I want different event arguments for each event type. I don’t see a way to keeping a single collection of subscribers that all have different event arguments. I can use a base type for the event arguments, but then the event handlers are forced to use the base type and the subscribers has to cast the event arguments to the concrete type (which I don’t want). I basically have something like this:

public abstract class PresentationEvent

{
    private readonly List<Action<IPresentationEventArgs>> _subscribers = new List<Action<IPresentationEventArgs>>();

    public void Subscribe(Action<IPresentationEventArgs> action)
    {
        _subscribers.Add(action);
    }

    public void Publish(IPresentationEventArgs message)
    {
        foreach (var sub in _subscribers)
        {
            sub.Invoke(message);
        }
    }
}

 public class MessageChangedEvent : PresentationEvent
    {

    }

public static class EventBus 
    {
        private static readonly Dictionary<Type, PresentationEvent> _mapping = new Dictionary<Type, PresentationEvent>();

        private static PresentationEvent GetPresentationEvent<T>() where T : PresentationEvent, new()
        {
            if (_mapping.ContainsKey(typeof(T)))
            {
                return _mapping[typeof(T)];
            }

            var presEvent = new T();
            _mapping.Add(typeof(T), presEvent);

            return presEvent;
        }

        public static void Subscribe<T>(Action<IPresentationEventArgs> action) where T: PresentationEvent, new()
        {
            var presEvent = GetPresentationEvent<T>();
            presEvent.Subscribe(action);
        }

        public static void Publish<T>(IPresentationEventArgs args) where T : PresentationEvent, new()
        {
            var presEvent = GetPresentationEvent<T>();
            presEvent.Publish(args);
        }
    }

But when handling the event, I am forced to do this:

private void OnMessageChanged(IPresentationEventArgs x)
        {
// do cast here
        }

instead of:

 private void OnMessageChanged(MessageChangedEventArgs args)
        {
            label1.Text = args.Message;
        }

Other than keeping some event dictionary with different lists for each event type, I am not sure how I would handle this. I know there are third party libraries out there but I’d prefer to write the code myself. I’ve also looked at similar questions and don’t find anything. If anyone has a suggestion on how to solve this problem or other recommendations it would be appreciated.

  • 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-31T00:15:00+00:00Added an answer on May 31, 2026 at 12:15 am

    If you add another generic parameter you can have strongly typed events.

    public interface IPresentationEventArgs { }
    
    public abstract class PresentationEvent<TPresentationEventArgs> where TPresentationEventArgs : IPresentationEventArgs
    {
        private readonly List<Action<TPresentationEventArgs>> _subscribers = new List<Action<TPresentationEventArgs>>();
    
        public void Subscribe(Action<TPresentationEventArgs> action)
        {
            _subscribers.Add(action);
        }
    
        public void Publish(TPresentationEventArgs message)
        {
            foreach (var sub in _subscribers)
            {
                sub.Invoke(message);
            }
        }
    }
    
    public class MessageChangedEventArgs : IPresentationEventArgs 
    {
        public string Message { get; set; }
    }
    
    public class MessageChangedEvent : PresentationEvent<MessageChangedEventArgs>
    {
    
    }
    
    public static class EventBus
    {
        private static readonly Dictionary<Type, Func<Object>> _mapping = new Dictionary<Type, Func<Object>>();
    
        private static T GetPresentationEvent<T, TArgs>()
            where T : PresentationEvent<TArgs>, new()
            where TArgs : IPresentationEventArgs
        {
            if (_mapping.ContainsKey(typeof(T)))
            {
                return _mapping[typeof(T)]() as T;
            }
    
            var presEvent = new T();
            _mapping.Add(typeof(T), () => presEvent);
    
            return presEvent;
        }
    
        public static void Subscribe<T, TArgs>(Action<TArgs> action) where T : PresentationEvent<TArgs>, new()
            where TArgs : IPresentationEventArgs
        {
            var presEvent = GetPresentationEvent<T, TArgs>();
            presEvent.Subscribe(action);
        }
    
        public static void Publish<T, TArgs>(TArgs args) where T : PresentationEvent<TArgs>, new()
            where TArgs : IPresentationEventArgs
        {
            var presEvent = GetPresentationEvent<T, TArgs>();
            presEvent.Publish(args);
        }
    }
    

    So a small test program to demonstrate how this could work:

    class Program
    {
        static void OnMessageChanged(MessageChangedEventArgs args)
        {
            Console.WriteLine(args.Message);
        }
    
        static void Main(string[] args)
        {
            EventBus.Subscribe<MessageChangedEvent, MessageChangedEventArgs>(OnMessageChanged);
            EventBus.Publish<MessageChangedEvent, MessageChangedEventArgs>(new MessageChangedEventArgs{ Message = "Hello world."});
    
            Console.ReadKey();
        }
    }
    

    You have the added overhead of calling subscribe and publish with 2 generic parameters, but on the other hand you can bind an event to specific eventArgs and consumers cannot pass any arbitrary eventArgs for a given event. They would need to match.

    Here is a small optimisation. Rather than create your own list of actions, you could just add actions up and allow the power of a Multicast delegate to keep track of all the actions for you. For example:

    public abstract class PresentationEvent<TPresentationEventArgs> where TPresentationEventArgs : IPresentationEventArgs
    {
        private Action<TPresentationEventArgs> _actions = args => { };
    
        public void Subscribe(Action<TPresentationEventArgs> action)
        {
            _actions += action;
        }
    
        public void Publish(TPresentationEventArgs message)
        {
            _actions(message);
        }
    }
    

    Update

    Here is one other way you can do the subscription. But no matter which approach you take, if you want statical linking and compile time checks then you will need to supply 2 type arguments.

    • 1 type argument to specify the type of event you want to subscribe to.
    • 1 type argument to cast the method being subscribed to as an Action since the compiler cannot infer it being the case just from the method signature

    With that in mind here is another way, but you don’t avoid having to specify 2 arguments.

    public static class IPresentationEventArgsExtensions
    {
        public static void SubscribeTo<TEvent, TArgs>(this TEvent target, Action<TArgs> action)
            where TArgs : IPresentationEventArgs
            where TEvent : PresentationEvent<TArgs>, new()
        {
            EventBus.Subscribe<TEvent, TArgs>(action);
        }
    }
    
    // Use
     Action<MessageChangedEventArgs> messageChangedMethod = OnMessageChanged; // The compiler cannot infer that OnMessageChanged is a Action<IPresentationEventArgs>
     new MessageChangedEvent().SubscribeTo(messageChangedMethod);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have some event listeners setup to log all insert/update/delete actions that happen.
I have a ToggleButtonBar with a DataProvider setup like this: <mx:ToggleButtonBar itemClick=clickHandler(event); selectedIndex=0> <mx:dataProvider>
I'm trying to setup an automatic event countdown. What I want to do is
I want to setup a click event trigger in jQuery for certain anchor tags.
Dojo seems to have a useful feature in that it can setup event handlers
I setup a link element and called its click event in jQuery but the
I have setup a property and implement INotifyPropertyChanged like so... public event PropertyChangedEventHandler PropertyChanged;
I use $(window).bind( ... ) to set up event handlers, but for some reason
Here is my issue, it appears that all the communication lines for the PIC
In jquery an event hadler's binding is the event generating DOM element (this points

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.