I am creating a common interface to work as a command template:
public interface IGenericComponent<T> where T : IVisitableObject { void Update(T msg); }
Then I will have another class that I will spend a bunch of implementations of this interface (each with its own type). There I would have a dictionary that would list the list of commands as follows:
private Dictionary<MessageType, List<IGenericComponent>> _components;
This creates a compilation error because I did not set the type for IGenericComponent. I have a thread that calls the Update method and a method for subscribing (insert the component into 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 omit the parameterized type when I instantiate the type. So ... I would like to know if it is possible to do this in C #, so he would suggest that its generic type (IVisitableObject). Or if you know the best way to solve this problem ... I solved it not the way I would like. I removed the generics from the interface and used the generic type IVisitableObject as a parameter to the Update method. Thanks in advance.
source share