title: Converting generic type to its base and vice-versa
body: <p>Can someone help me with the conversion I am facing in enclosed code ... I commented the lines of code, where I am having problem. Is this even the right way to achieve this ... what I am trying to do, is forward responses of specified type to provided callback.</p>
  
  <blockquote>
    <p><strong>EDIT 1</strong></p>
    
    <p>I forgot to mention that Response and AFResponse are abstract classes,
    which goes: >Response -> AFResponse -> concrete implementations of AF
    layer messages</p>
  </blockquote>
  
  <pre><code>public class MessageBinder
  {
      private class Subscriber&lt;T&gt; : IEquatable&lt;Subscriber&lt;T&gt;&gt; where T : Response
      {
          ...
      }
  
      private readonly Dictionary&lt;Type, List&lt;Subscriber&lt;Response&gt;&gt;&gt; bindings;
  
      public MessageBinder()
      {
          this.bindings = new Dictionary&lt;Type, List&lt;Subscriber&lt;Response&gt;&gt;&gt;();
      }
  
      public void Bind&lt;TResponse&gt;(short shortAddress, Action&lt;ZigbeeAsyncResponse&lt;TResponse&gt;&gt; callback)
          where TResponse : Response
      {
          List&lt;Subscriber&lt;TResponse&gt;&gt; subscribers = this.GetSubscribers&lt;TResponse&gt;();
          if (subscribers != null)
          {
              subscribers.Add(new Subscriber&lt;TResponse&gt;(shortAddress, callback));
          }
          else
          {
              var subscriber = new Subscriber&lt;TResponse&gt;(shortAddress, callback);
  
              // ERROR: cannot convert from 'List&lt;Subscriber&lt;TResponse&gt;&gt;' to 'List&lt;Subscriber&lt;Response&gt;&gt;' ... tried LINQ Cast operator - does not work either
              this.bindings.Add(typeof(TResponse), new List&lt;Subscriber&lt;TResponse&gt;&gt; { subscriber });
          }
      }
  
      public void Forward&lt;TResponse&gt;(TResponse response)
          where TResponse : Response
      {
          var subscribers = this.GetSubscribers&lt;TResponse&gt;();
          if (subscribers != null)
          {
              Subscriber&lt;TResponse&gt; subscriber;
              Type responseType = typeof (TResponse);
  
              if (responseType.IsSubclassOf(typeof (AFResponse)))
              {
                  // ERROR: Cannot convert type 'TResponse' to 'AFResponse' ... tried cast to object first, works, but is this the right way?
                  var afResponse = (AFResponse)response;
                  subscriber = subscribers.SingleOrDefault(s =&gt; s.ShortAddress == afResponse.ShortAddress);
              }
              else
              {
                  subscriber = subscribers.First();
              }
  
              if (subscriber != null)
              {
                  subscriber.Forward(response);
              }
          }
      }
  
      private List&lt;Subscriber&lt;TResponse&gt;&gt; GetSubscribers&lt;TResponse&gt;() where TResponse : Response
      {
          List&lt;Subscriber&lt;Response&gt;&gt; subscribers;
          this.bindings.TryGetValue(typeof(TResponse), out subscribers);
  
          // ERROR: How can I cast List&lt;Subscriber&lt;Response&gt;&gt; to List&lt;Subscriber&lt;TResponse&gt;&gt;?
          return subscribers;
      }
  }
  </code></pre>
  
  <p>Thank you for any help :)</p>
  
  <blockquote>
    <p><strong>EDIT 2</strong></p>
    
    <p>I changed the code according to @Bojan answer, and it is working. But
    I am curious, why the Dictionary can't hold the base class of all
    Response messages? Is there even a way to accomplish that, or did I
    just tried to push my head through a wall?</p>
    
    <p><strong>EDIT 3</strong></p>
    
    <p>Now I am facing another problem ... when message arrive, it is
    composed of byte array, >which goes to message factory which resolves
    and builds it:</p>
  </blockquote>
  
  <pre><code>public static T Build&lt;T&gt;(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});
  }
  </code></pre>
  
  <blockquote>
    <p>Then an <strong>OnAsyncResponseReceived(Response response)</strong> event handler
    is invoked, which then forwards message to the subscriber of this
    message, if any. The problem is now that if I subscribe to (sub layer
    of response are: AFResponse, SystemResponse, etc...)
    SystemResetResponse which is subclass of SystemResponse which is
    subclass of Response, that I must cast that response from
    Response(base) type all the way to the concrete type, which is
    SystemResetResponse in order the Forwarder in MessageBinder can find
    subscribers of this message type, and forwards it.</p>
    
    <p>There are many types and casting by hand would be an overkill ... is
    there a way around this, or even a better way to design this type of
    system?</p>
    
    <p><strong>EDIT 4</strong></p>
    
    <p>I changed a code like this ... is this the right way to do this, is
    there any other, better way and over all, am I trying to solve the
    problem the right way or is there better way to handle this?</p>
  </blockquote>
  
  <pre><code>private void OnAsyncResponseReceived(Response response)
  {
      dynamic resolvedResponse = Convert.ChangeType(response, response.GetType());
      messageBinder.Forward(resolvedResponse);
  }
  </code></pre>