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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:48:14+00:00 2026-06-12T15:48:14+00:00

I have the following base interface public interface IHandler{ void Handle(IMessage message); } and

  • 0

I have the following base interface

public interface IHandler{
  void Handle(IMessage message);
}

and an generic interface inheriting the base interface

public interface IHandler<TMessage> : IHandler where TMessage : IMessage{
  void Handle(TMessage message);
}

My classes can implement the interface IHandler<TMessage> mutiple times. IMessage is an base interface for messages and isn´t relevant here. Currently i´m implementing the interfaces as follows.

public class ExampleHandler : IHandler<ExampleMessage>, IHandler<OtherExampleMessag>{

  void IHandler.Handle(IMessage message){
    ExampleMessage example = message as ExampleMessage;

    if (example != null) {
      Handle(example);
    }
    else {
      OtherExampleMessage otherExample = message as OtherExampleMessage;

      if (otherExample != null) {
        Handle(otherExample);
      }
  }

  public void Handle(ExampleMessage) {
   //handle message;
  }

  public void Handle(OtherExampleMessage) {
   //handle message;
  }
}

What bothers me is the way i have to implement the Handle(IMessage) method, cause in my opinion its many redundant code, and i have to extend the method each time when i implement a new IHandler<TMessage> interface on my class.

What i´m looking for is a more generic way to implement the Handle(IMessage) method (maybe in a base class for Handlers), but i´m currently stuck how to do that.

  • 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-12T15:48:15+00:00Added an answer on June 12, 2026 at 3:48 pm

    You can use the new dynamic keyword to move the overload resolution to the DLR:

    void IHandler.Handle(IMessage message)
    {
        dynamic d = message;
        Handle(d);
    }
    

    Please note that this will fail at runtime with a RuntimeBinderException if the message passed in is not valid for your class.
    To avoid this exception you can add a Handler for all unknown message types:

    private void Handle(object unknownMessage)
    {
        // Handle unknown message types here.
    }
    

    To implement IHandler.Handle in a base class, you need to do a little bit more work:

    public class BaseHandler : IHandler
    {
        void IHandler.Handle(IMessage message)
        {
            dynamic d = message;
            Handle(d);
        }
    
        private void Handle<TMessage>(TMessage message) where TMessage : IMessage
        {
            var handler = this as IHandler<TMessage>;
            if(handler == null)
                HandleUnknownMessage(message);
            else
                handler.Handle(message);
        }
    
        protected virtual void HandleUnknownMessage(IMessage unknownMessage)
        {
            // Handle unknown message types here.
        }
    }
    

    Your specific handler would than look like this:

    public class ExampleHandler : BaseHandler,
                                  IHandler<ExampleMessage>,
                                  IHandler<OtherExampleMessage>
    {
        public void Handle(ExampleMessage message)
        {
            // handle ExampleMessage here
        }
    
        public void Handle(OtherExampleMessage message)
        {
            // handle OtherExampleMessage here
        }
    }
    

    This code now works like this:

    1. The DLR calls the generic BaseHandler.Handle<TMessage> method with the real message type, i.e. TMessage will not be IMessage but the concrete message class like ExampleMessage.
    2. In this geneirc handler method, the base class tries to case itself to a handler for the specific message.
    3. If that is not successful, it calls HandleUnknownMessage to handle the unknown message type.
    4. If the cast is successful, it calls the Handle method on the specific message handler, effectifly delegating the call to the concrete Handler implementation.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following generic question Here is a my generic message interface. public
I have the following items in my solution: public interface IHandle<TEvent> { void Handle(TEvent
I have the following base interface public interface IBaseAction { bool CanAct(...) } and
I have the following interfaces defined in a base library project: public interface IWorkContext
I have the following classes: public interface IWaybillDocument { long DocumentId { get; set;
I have the following wrapper: public interface ITransactionScopeWrapper : IDisposable { void Complete(); }
Suppose I have the following (trivially simple) base class: public class Simple { public
I have the following classes in my ActiveRecord model: def Property < ActiveRecord::Base #
I have the following classes: class Vigil < ActiveRecord::Base after_update :do_something_cool private def do_something_cool
Let's say I have the following class hierarchy (base interface included): IAction -> (abstract)

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.