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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:22:37+00:00 2026-05-24T18:22:37+00:00

I created a very simple event publisher and it looks like this. public class

  • 0

I created a very simple event publisher and it looks like this.

public class EventPublisher
{
    private readonly IList<Func<IHandle>> _subscribers;

    public EventPublisher(IList<Func<IHandle>> subscribers)
    {
        _subscribers = subscribers;
    }

    public void Publish<TPayload>(TPayload payload)
        where TPayload : class
    {
        var payloadHandlers = _subscribers.OfType<Func<IHandle<TPayload>>>();

        foreach (var payloadHandler in payloadHandlers)
        {
            payloadHandler().Handle(payload);
        }
    }
}

Here is what I have to publish messages.

var subscribers = new List<Func<IHandle>> {() => new SomeHandler()};
var eventPublisher = new EventPublisher(subscribers);

eventPublisher.Publish(new SomeMessage { Text = "Some random text..." });

The issue I’m having is that publishing a message isn’t finding any handlers that can handle the payload. This makes sense because I registered my subscribers as Func<IHandle> instead of Func<IHandle<T>>.

The interface my handler classes are inheriting from is from Caliburn.Micro.EventAggregator and it looks like this.

public interface IHandle {}

public interface IHandle<TMessage> : IHandle {
    void Handle(TMessage message);
}

What must the type of _subscribers be to handle the IHandle<> where the generic type can be any concrete type?

  • 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-24T18:22:38+00:00Added an answer on May 24, 2026 at 6:22 pm

    Assuming you’re using .NET 4, I’d expect this to work:

    var subscribers = new List<Func<IHandle<SomeMessage>>> {() => new SomeHandler()};
    

    then to get covariance:

    public EventPublisher(IEnumerable<Func<IHandle>> subscribers)
    {
        _subscribers = subscribers.ToList();
    }
    

    That lets you subscribe with any sequence of things compatible with Func<IHandle>. Note that it does change the semantics significantly, in that the set of subscribers will be fixed after construction. Personally I’d regard that as a good thing, but it may not suit you.

    Alternatively, does the EventPublisher itself have to work with multiple types? Could you make it EventPublisher<T> and use IHandle<T> everywhere, making Publish non-generic? This may or may not be feasible based on how you want to use this – both options make sense.

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

Sidebar

Related Questions

public function actionCheckout() { echo Hello World!; } I just created this very simple
Ok, so i created a very simple splash screen using this tutorial: http://p-xr.com/android-tutorial-how-to-make-a-basic-splash-screen/ The
This seems like a very simple problem, but I cannot get a scrollbox to
I've created a very simple Windows Service projects with this tutorial . I've build
I have created very simple app with persistence context (hibernate as provider) to read
I created two very simple Heroku apps to test out the service, but it's
What I have created a very simple asp.net web service using .NET framework 3.5,
I've created a very simple Enterprise Application project with about 7 entity beans and
I've created a very simple drop down list in excel. It holds a sequence
I have created a very simple sharepoint timer job. All i want it to

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.