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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:08:24+00:00 2026-06-02T22:08:24+00:00

I am trying to pass messages between several classes that communicate through interface. However,

  • 0

I am trying to pass messages between several classes that communicate through interface. However, as I like to go as generic as possible, I ran into problems because the message type of incoming messages may be different from the outgoing type. I pasted some code to make it clearer.

The code below does not compile because the interface implementation passes a different type than the type of the blocking collection to which it is supposed to add incoming messages. I want to be able to send types potentially different from incoming types (incoming types obviously always match the type of the elements in the blocking collection). Can I somehow get around any sort of casting or parsing even if that means I need to redesign my interface or class?

I am still quite fresh when it comes to working with interfaces and struggled with recursions, stack overflow errors, and the like. So, if you have suggestions what I can improve design wise or just a quick fix then please help me to learn. Am very eager to understand how to implement a better pattern.

Thanks

public interface IClientMessaging
{
    void MessagePassing<U>(U message);
}

public class InProcessMessaging<T> : IClientMessaging
{
    private Dictionary<Type, List<IClientMessaging>> Subscriptions;
    public BlockingCollection<T> MessageBuffer;

    public InProcessMessaging(Dictionary<Type, List<IClientMessaging>> subscriptions)
    {
        //Setup Message Buffer
        MessageBuffer = new BlockingCollection<T>();

        //Subscribe
        Type type = typeof(T);
        if (subscriptions.Keys.Contains(type))
        {
            subscriptions[type].Add(this);
        }
        else
        {
            subscriptions.Add(type, new List<IClientMessaging>());
            subscriptions[type].Add(this);
        }

        Subscriptions = subscriptions;
    }

    public void SendMessage<U>(U message)
    {
        //Send message to each subscribed Client
        List<IClientMessaging> typeSubscriptions = Subscriptions[typeof(U)];
        foreach (IClientMessaging subscriber in typeSubscriptions)
        {
            subscriber.MessagePassing<U>(message);
        } 
    }

    public T ReceiveMessage()
    {
        return MessageBuffer.Take();
    }

    public bool ReceiveMessage(out T item)
    {
        return MessageBuffer.TryTake(out item);
    }

    //Interface Implementation
    public void MessagePassing<U>(U message)
    {
        MessageBuffer.Add(message); //<-"Cannot convert from U to T" [this is because I want
                                    //to send messages of a different type than the receiving 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-06-02T22:08:25+00:00Added an answer on June 2, 2026 at 10:08 pm

    I’m struggling to understand your goal here, but perhaps MessagePassing<U>(U message) should be MessagePassing(U message) and interface IClientMessaging should be interface IClientMessaging<U>.

    Then InProcessMessaging<T, U> : IClientMessaging<U> – but I don’t see why InProcessMessaging implements IClientMessaging AND manages subscriber lists of IClientMessaging. Seems to me that one class would manage the subscribers and another IS a subscriber (IClientMessaging).

    You say U and T are different types. Well – are they related? Is one wrapper for the other? Sounds like maybe U is either a wrapper for T, a generic class itself that contains the T but adds extra info. In that case, void MessagePassing<T>(Wrapper<T> message);

    UPDATES

    Based on the comments so far …

    interface IClientMessage {}
    
    interface IClientMessage<U> : IClientMessage { /* ... */ }
    

    But rename those to:

    interface IConsumer {} // (Or ISubscriber?)
    
    interface IConsumer<TConsumed> : IConsumer{ /* ... */ }
    

    and add:

    interface IGenerator { }
    
    interface IGenerator <TGenerated> : IGenerator { 
        event EventHandler<TGenerated> ItemGenerated; 
    }
    

    Then:

    class Manager
    {
        Dictionary<TConsumed, IConsumer> consumers = new ...
    
        /* Code for attaching ItemGenerated event handlers to clients */
    }
    
    class MyClient : IGenerator<string>, IConsumer<Foo>, IConsumer<Bar>
    {
        event IGenerator<string>.ItemGenerated ...
    
        void IConsumer<Foo>.Consume(...) ...
    
        void IConsumer<Bar>.Consume(...) ...
    }
    

    Yes, this would use reflection to invoke IConsumer<TConsumed>.Consume(). Or you can leave off the generics and just use object as your types. Better yet, IClientMessage can have a Consume(object message) which in your implementation can ensure that object is a TConsumed before attempting to process it.

    You could otherwise create direct client-to-client links through C# events, but you seem intent on a central dispatcher. It is the central dispatchers need to keep track of these different and unbounded number of types that is either going to require reflection OR be unaware of the types being passed (as described in the previous paragraph)

    You should look at Reactive Extensions and the Observer pattern for ideas as well.

    I removed my comments because it was getting too chatty.

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

Sidebar

Related Questions

I'm trying to pass an array through jquery ajax call. However, I need to
I'm trying to use this jQuery postMessage plugin to pass messages between a couple
Base class MessageHandler has derived classes. They would like to pass messages to each
I'm trying to pass a function as an option through a jquery plugin that
I am trying to implement something like Ruby on Rail's ActionDispatch::Flash to pass messages
I'm trying to pass messages between 2 threads using a queue, but I haven't
(Edit: I've since realised that perhaps it's not possible to pass arrays as parameters
I'm trying to pass a variable from my main swf to another one that's
I'm trying to pass data between a JS file and a PHP file, but
I'm trying to figure out a user friendly way to pass messages to users

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.