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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:59:24+00:00 2026-05-28T01:59:24+00:00

I’m creating a generic interface to work as command pattern: public interface IGenericComponent<T> where

  • 0

I’m creating a generic interface to work as command pattern:

public interface IGenericComponent<T> where T : IVisitableObject
{
    void Update(T msg);
}

Then, I’ll have another class that I’ll hold a bunch of implementations of this interface (each one with its own type). There I would have a dictionary to put the list of commands to execute like this:

private Dictionary<MessageType, List<IGenericComponent>> _components;

This generates a compilation error because I don’t put the type for the IGenericComponent. I have a thread that calls the Update method and a method to subscribe (inserta an component to the dictionary):

public void Subscribe<T>(MessageType messageType, IGenericComponent<T> component) where T : IVisitableObject, new()
    {
        lock (_monitorDictionary)
        {
            List<IGenericComponent> subscribedList;
            if (_components.TryGetValue(messageType, out subscribedList))
            {
                subscribedList.Add(component);
                IVisitableObject firstUpdate;
                if(_messageBuffer.TryGetValue(messageType, out firstUpdate))
                    component.Update((T)firstUpdate);
            }
            else
            {
                subscribedList = new List<IGenericComponent>();
                subscribedList.Add(component);
                _components[messageType] = subscribedList;
            }
        }
    }

    private void ProcessQueue()
    {
        while (true)
        {
            IVisitableObject msg;
            lock (_monitorQueue)
            {
                msg = _queue.Dequeue();
            }
            List<IGenericComponent> components;
            lock(_monitorDictionary)
            {
                components = _components[msg.MsgType];
            }
            if(components!= null)
            {
                foreach (IGenericComponent genericComponent in components)
                    genericComponent.Update(msg);
            }
        }
    }

This code does not compile…
I came from Java programming, and in Java I can ommit the parametrized type when I instantiate the type. So… I would like to know if is it possible to do that in C# so it would assume that its the generic type (IVisitableObject). Or if you know a better way to solve this problem…
The way I’ve solved this is not the way I would like to use. I’ve removed generics from the interface and used the generic type IVisitableObject as the parameter of Update method.
Thanks in advance.

  • 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-28T01:59:25+00:00Added an answer on May 28, 2026 at 1:59 am

    I’ve used the approach in Jason’s answer and it works fine, especially if you can hide the cast from IVisitableObject to T in a base class. But if you want to avoid forcing classes to implement the non-generic interface, you can use this pattern. Store your subscribers as a List<object> and use a helper class (Dispatcher) to send the message.

    public interface IVisitableObject { }
    public interface IGenericComponent<T> where T : IVisitableObject
    {
        void Update(T msg);
    }
    
    abstract class Dispatcher
    {
        protected Dispatcher() { }
    
        public abstract void Dispatch(IVisitableObject message, IEnumerable<object> subscribers);
    
    
        static Dictionary<Type, Dispatcher> dispatchers = new Dictionary<Type, Dispatcher>();
    
        static Dispatcher GetDispatcherFor(IVisitableObject message)
        {
            Type type = message.GetType();
    
            if (!dispatchers.ContainsKey(type))
            {
                Type closedType = typeof(Dispatcher<>).MakeGenericType(message.GetType());
                object dispatcher = Activator.CreateInstance(closedType);
                dispatchers[type] = (Dispatcher)dispatcher;
            }
    
            return dispatchers[type];
        }
    }
    
    class Dispatcher<T> : Dispatcher where T : IVisitableObject
    {
        public override void Dispatch(IVisitableObject message, IEnumerable<object> subscribers)
        {
            var msg = (T)message;
            foreach (var subscriber in subscribers.OfType<IGenericComponent<T>>())
            {
                subscriber.Update(msg);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.