Can someone help me with the conversion that I came across in the attached code ... I commented out the lines of code where I have a problem. This is even the right way to achieve this ... what I'm trying to do is respond the specified type to the provided callback.
EDIT 1
I forgot to mention that Response and AFResponse are abstract classes that goes:> Response -> AFResponse -> concrete implementations of AF level messages
public class MessageBinder { private class Subscriber<T> : IEquatable<Subscriber<T>> where T : Response { ... } private readonly Dictionary<Type, List<Subscriber<Response>>> bindings; public MessageBinder() { this.bindings = new Dictionary<Type, List<Subscriber<Response>>>(); } public void Bind<TResponse>(short shortAddress, Action<ZigbeeAsyncResponse<TResponse>> callback) where TResponse : Response { List<Subscriber<TResponse>> subscribers = this.GetSubscribers<TResponse>(); if (subscribers != null) { subscribers.Add(new Subscriber<TResponse>(shortAddress, callback)); } else { var subscriber = new Subscriber<TResponse>(shortAddress, callback);
Thanks for any help :)
EDIT 2
I changed the code according to @Bojan's answer and it works. But I'm curious why the Dictionary cannot contain the base class of all Response Messages? Is there a way to do this, or am I just trying to push my head through the wall?
EDIT 3
Now I am faced with another problem ... when the message arrives, it consists of a byte array> that goes into the factory message, which resolves and builds it:
public static T Build<T>(Packet packet) where T : Response { Type resolvedType; if (!dependencyMap.TryGetValue(packet.MessageId, out resolvedType)) { var str = String.Format("Could not resolve message. Message info: CMD0: {0}, CMD1: {1}, MessageID: {2}", packet.Cmd0, packet.Cmd1, packet.MessageId); Debug.WriteLine(str); throw new MessageNotFoundException(str); } ConstructorInfo firstConstructor = resolvedType.GetConstructors().First(); return (T) firstConstructor.Invoke(new object[] {packet}); }
Then the OnAsyncResponseReceived (Response response) event handler, which then passes the message to the subscriber of this message, if any. The problem is that if I subscribe to (sub layer response: AFResponse, SystemResponse, etc.) SystemResetResponse, which is a subclass of SystemResponse, which is a subclass of Response, I have to drop this answer from the Response type (base) to a specific type, which SystemResetResponse so that the Forwarder in MessageBinder can find subscribers to this type of message and forwards it.
There are many types, and manual casting will be redundant ... is there a way around this, or even a better way to develop this type of system?
EDIT 4
I changed code like this ... is this the right way to do this, is there any other, better way and over everything, am I trying to solve a problem in the right direction or is there a better way to handle this?
private void OnAsyncResponseReceived(Response response) { dynamic resolvedResponse = Convert.ChangeType(response, response.GetType()); messageBinder.Forward(resolvedResponse); }