title: How to test the type of a generic interface?
body: <p>I'm not sure if the title makes sense, but I hope you can understand my question with some code.</p>
  
  <p>Given the following code for a publish/subscribe framework.</p>
  
  <pre><code>type
    IMessage = interface
      ['{B1794F44-F6EE-4E7B-849A-995F05897E1C}']
    end;
  
    ISubscriber = interface
      ['{D655967E-90C6-4613-92C5-1E5B53619EE0}']
    end;
  
    ISubscriberOf&lt;T: IMessage&gt; = interface(ISubscriber)
      procedure Consume(const message: T);
    end;
  
    TMessageService = class
    private
      FSubscribers: TList&lt;ISubscriber&gt;;
    public
      constructor Create;
      destructor Destroy; override;
      procedure SendMessage(const message: IMessage);
      procedure Subscribe(const Subscriber: ISubscriber);
      procedure Unsubscribe(const Subscriber: ISubscriber);
    end;
  </code></pre>
  
  <p>That would be used like this:</p>
  
  <pre><code>TMyMessage = class(TInterfacedObject, IMessage);
  
  TMySubscriber = class(TInterfacedObject, ISubscriberOf&lt;TMyMessage&gt;)
    procedure Consume(const Message: TMyMessage);
  end;
  
  TMyOtherMessage = class(TInterfacedObject, IMessage);
  
  TMyOtherSubscriber = class(TInterfacedObject, ISubscriberOf&lt;TMyOtherMessage&gt;)
    procedure Consume(const Message: TOtherMessage);
  end;
  </code></pre>
  
  <p>How can I loop the subscribers list and send the message to the proper subscribers? </p>
  
  <p>The subscribers list will have all subscribers for all types of messages. The SendMessage have to find the subscribers for the type of message provided as param and send it to whom implements the proper interface to consume that type of message.</p>
  
  <pre><code>  procedure TMessageService.SendMessage(const message: IMessage);
    var
      Subscriber: ISubscriber;
    begin
      for Subscriber in FSubscribers do
      begin
        // How to send the message only to the subscribers of the correspondent type of message
      end;
    end;
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>BTW, this code is based on this <a href="http://www.finalbuilder.com/Resources/Blogs/tabid/458/EntryId/278/The-quest-for-the-perfect-publish-subscribe-mechanism-in-Delphi.aspx" rel="nofollow">blog post</a>.</p>