How to store and locate multiple interface types within a Delphi TInterfaceList

Posted by Brian Frost on Stack Overflow See other posts from Stack Overflow or by Brian Frost
Published on 2010-06-02T15:32:19Z Indexed on 2010/06/02 15:54 UTC
Read the original article Hit count: 216

Filed under:
|
|

Hi, I'm storing small interfaces from a range of objects into a single TInterfaceList 'store' with the intention of offering list of specific interface types to the end user, so each interface will expose a 'GetName' function but all other methods are unique to that interface type. For example here are two interfaces:

  IBase = interface
    //----------------------------------------
    function GetName : string;
    //----------------------------------------
  end;

  IMeasureTemperature = interface(IBase)
    //------------------------------------
    function MeasureTemperature : double;
    //----------------------------------------
  end;

  IMeasureHumidity = interface(IBase)
    //----------------------------------------
    function MeasureHumidity: double;
    //----------------------------------------
  end;

I put several of these interfaces into a single TInterfaceList and then I'd like to scan the list for a specific interface type (e.g. 'IMeasureTemperature') building another list of pointers to the objects exporting those interfaces. I wish to make no assumptions about the locations of those objects, some may export more than one type of interface. I know I could do this with a class hierarchy using something like:

  If FList[I] is TMeasureTemperature then ..

but I'd like to do something simliar with an interface type, Is this possible?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about interface