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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:32:10+00:00 2026-06-09T10:32:10+00:00

Why doesn’t this work?! public interface IBus { void Subscribe<T>(ISubscribe<T> subscriber) where T :

  • 0

Why doesn’t this work?!

    public interface IBus
    {
        void Subscribe<T>(ISubscribe<T> subscriber) where T : class, IEvent;
        void Send<T>(IEvent @event) where T : class, IEvent;
    }

    class InMemoryEventBus : IBus
    {
        private readonly IDictionary<ISubscribe<IEvent>, Type> _subscribers;

        public InMemoryEventBus()
        {
            _subscribers= new Dictionary<ISubscribe<IEvent>, Type>();
        }

        public void Subscribe<T>(ISubscribe<T> subscriber) where T : class, IEvent
        {
            _subscribers.Add(subscriber, typeof(T));
        }

        public void Send<T>(IEvent @event) where T : class, IEvent
        {
            foreach (var subscriber in _subscribers.Where(subscriber => subscriber.Value == typeof(T)))
            {
                subscriber.Key.Handle(@event);
            }
        }
    }

    public interface IEvent
    {
        Guid EventId { get; set; }
    }

    public interface ISubscribe<T> where T : IEvent
    {
        void Handle(T @event);
    }

    public class StockLevelDroppedBellowMinimumLevelEvent : IEvent
    {
        public Guid EventId { get; set; }

        public string Message { get; set; }
    }

I get:

cannot convert from 'IHandle<T>' to 'IHandle<IEvent>'
  • 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-09T10:32:11+00:00Added an answer on June 9, 2026 at 10:32 am

    EDIT: It was actually ISubscribe which needed to be covariant – but now that we can see the declaration of ISubscribe, you can’t make it covariant – only contravariant.

    In general, types aren’t generically variant. For example, an ICollection<string> isn’t an ICollection<object> – which is a good job, otherwise this would compile:

    ICollection<string> strings = new List<string>();
    ICollection<object> objects = strings; // Fortunately this isn't valid
    objects.Add(new Button()); // This should be fine of course...
    string x = strings[0]; // But what would this do?!
    

    In your case, I believe you’ll need to change your dictionary – you’ll be able to guarantee that the “right” type of ISubscribe is used in each case. However, you’re using the dictionary in a very odd way – you should reverse the key and value types, like this:

    class InMemoryEventBus : IBus
    {
        private readonly IDictionary<Type, object> _subscribers;
    
        public InMemoryEventBus()
        {
            _subscribers = new Dictionary<Type, object>();
        }
    
        public void Subscribe<T>(ISubscribe<T> subscriber) where T : IEvent
        {
            _subscribers.Add(typeof(T), subscriber);
        }
    
        public void Send<T>(IEvent @event) where T : class, IEvent
        {
            object value;
            if (_subscribers.TryGetValue(typeof(T), out value))
            {
                 ISubscriber<T> subscriber = (ISubscriber<T>) value;
                 subscriber.Handle(@event);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This doesn't seem to work. Is it possible? public interface IInterface1 {} public interface
This doesn't work: interface TestInterface { public function testMethod(); } interface TestInterface2 { public
I am trying to work out why this works Public Interface IFullService Inherits ISerializableObjectLayerService,
I just discovered, quite by accident, that this seems to work: Public Interface Ix
I have an interface like this: public interface BatchSynchronisedPool<R extends Runnable> { void execute(R
Why does this not work? Do I not understand delegate covariance correctly? public delegate
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
Why doesn't this work? Some Usercontrol (name: myUserControl) with an image control (name: myImage):
why doesn't this work? MsgBox(F6D8C47B-46E6-4E93-A393-00085ACA2242).ToString.Replace(-, )
Why doesn't this work? // spriteArray is an NSMutableArray int spriteWidth = [spriteArray objectAtIndex:0].contentSize.width;

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.