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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:03:47+00:00 2026-06-01T11:03:47+00:00

I have the following interface public interface IHandler<T> { void Handle(T myObject); } I’d

  • 0

I have the following interface

public interface IHandler<T>
{
    void Handle(T myObject);
}

I’d like to have a HandlersManager class with holds a mapping between object types to their corresponding handler, but I’m not sure how I’m supposed to define the object that hold this mapping.

For example, what I’d like to have is this:

typeof(string)  --> instance of IHandler<string>
typeof(MyClass) --> instance of IHandler<MyClass>

The best thing I got so far was to define Dictionary<Type, object> for the mapping, but in this case I would have to cast the value to IHandler<T> every time I get it.

Is there a better solution or something that I have completely missed?

  • 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-01T11:03:48+00:00Added an answer on June 1, 2026 at 11:03 am

    That’s as good as it can get with only a generic IHandler<T> interface.

    In order to explore more options, we could define a non-generic version of the interface:

    public interface IHandler
    {
        void Handler(object myObject);
    }
    

    Then you could also define a generic abstract base class that implements both IHandler<T> and IHandler:

    public abstract class BaseHandler<T> : IHandler, IHandler<T>
    {
        public abstract void Handle(T myObject);
    
        void IHandler.Handle(object myObject)
        {
            ((IHandler<T>)this).Handle((T) myObject);
        }
    }
    

    At this point you can have an IDictionary<Type, IHandler> and you can directly call IHandler.Handle on the values you pull out of it:

    var obj = /* whatever */
    dictionary[obj.GetType()].Handle(obj);
    

    On the other hand we now have an additional interface and an abstract base class just to hide a cast from “user” code, which doesn’t sound very impressive.

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

Sidebar

Related Questions

I have the following items in my solution: public interface IHandle<TEvent> { void Handle(TEvent
I have the following interface: public interface SingleRecordInterface<T> { public void insert(T object); }
Consider I have the following interface: public interface A { public void b(); }
I have the following two interfaces: public interface IMembershipProvider { object Login(ILoginProviderParameters loginParameters); void
I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
Let's say that I have the following interface: public interface IMyService { void SimpleMethod(int
Assuming that I have the following interface and class: public interface IFooRepo : IDisposable
I have the following interface: public interface ILogger { void Debug(string message, params object[]
I have the following public interface public interface Bar{ public void DoStuff(); } with
I have the following interface: public interface Iface { void Sample1(); void Sample1(bool value);

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.