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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:09:44+00:00 2026-06-15T11:09:44+00:00

I have an object Receiver that is receiving/deserializing requests of many different types. I

  • 0

I have an object “Receiver” that is receiving/deserializing requests of many different types.

I would then like this receiver to make use of a “Dispatcher” object that would dispatch the request object to the correct request handler object (previously registered on the dispatcher), based on the type of the incoming request. Of course the type of the request is only known at runtime, not at compile time (would be too easy ;))

Here is some code to clarify things …

I have a marker interface for request types :

public interface IRequest {}

Then a dummy request type :

public class DummyRequest : IRequest {}

This is the generic interface to be used by a request handler for a request type T.

public interface IRequestHandler<in T> where T : IRequest
{
   void HandleRequest(T request);
}

And a concrete implementation being a request handler for DummyRequest requests :

public class DummyRequestHandler : IRequestHandler<DummyRequest>
{
   public void HandleRequest(DummyRequest request) { }
}

Here is my very basic implementation of a dispatcher making use of dynamic, but which is working only if the request type is known at compile time, but can’t work if the request type is known only at runtime (which is not of great interest in my context, but somehow shows the intent) :

public class Dispatcher
{
    private Dictionary<Type, dynamic> _handlers = new Dictionary<Type, dynamic>();

    public void Dispatch<T>(T requestObject) where T : IRequest
    {
       _handlers[requestObject.GetType()].HandleRequest(requestObject);
    }

    public void RegisterHandler<T>(IRequestHandler<T> handler) where T : IRequest
    {
       _handlers[typeof(T)] = handler;
    }
}

And the usage, considering dispatcher variable is a Dispatcher object :

Some code would register the handler on the dispatcher this way

dispatcher.RegisterHandler<DummyRequest>(new DummyRequestHandler());

And the receiver object would use the dispatcher to dispatch an incoming request this way :

dispatcher.Dispatch(new DummyRequest());            

Of course, here it works only if the request type is known at compile time, but in my context the request is received as an object type by the receiver such as :

// Here ReceiveMessage is not generic and therefore returns object type 
// (even if type is DummyRequest, it's just being returned as an object,
// i.e : "request is DummyRequest" returns true) 
// signature of ReceiveMessage is "object ReceiveMessage()"
var request = ReceiveMessage(); 

and I can’t call the dispatcher like this

dispatcher.Dispatch(request); 

I can’t find a way to work arround this without loosing type safety (for example not changing the HandleRequest signature to a non generic one taking an object) which I’d rather not lose.

I hope my question is clear and makes sense … there is maybe no real way, I don’t know.

Didn’t find anything interesting by googling arround “request dispatcher C#”. Maybe this kind of “pattern” is known under another name, but just make sense for me to call it a request dispatcher.

The finality behind this is to be able to handle new request types by just creating a new request handler for the specific request type and registering it on the dispatcher without having to change the code of the receiver, nor the dispatcher.

Thanks !

  • 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-15T11:09:45+00:00Added an answer on June 15, 2026 at 11:09 am

    You cannot get away from the fact that you do not know the object type until runtime so you need a design that copes with that.
    You should have a look at the factory pattern. If you google it you will find loads of examples. But in essence you have to figure out the type of the object in your code at runtime and figure out what object type to instantiate. This could take the form of using Reflection or it could simply be a switch statement inside the factory method.

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

Sidebar

Related Questions

I have a class Game that uses a class TCPServer . I would like
In my app I have a broadcast receiver that turns on GPS upon receiving
I have written this code for a Message Receiving class that uses a backgroundworker
I have an anonymous type object that I receive as a dynamic from a
Currently I have an object which I receive from a server, this contains two
I have two tables: object that has object_id column and avalues that have object_id
I think I read somewhere that some modules only have object oriented interfaces (
I have this object: stdClass Object ( [daily_inventoryID] => 1 [inventory_timestamp] => 2012-06-08 14:35:42
I have an IntentService that is making a network call and receiving back some
I have an application that implements something like a Chain of Responsibility in Python.

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.