我将一系列对象中的小接口(interface)存储到单个 TInterfaceList“存储”中,目的是向最终用户提供特定接口(interface)类型的列表,因此每个接口(interface)将公开一个“GetName”函数,但所有其他方法对于该接口(interface)类型是唯一的。例如这里有两个接口(interface):
IBase = interface
//----------------------------------------
function GetName : string;
//----------------------------------------
end;
IMeasureTemperature = interface(IBase)
//------------------------------------
function MeasureTemperature : double;
//----------------------------------------
end;
IMeasureHumidity = interface(IBase)
//----------------------------------------
function MeasureHumidity: double;
//----------------------------------------
end;
我将其中几个接口(interface)放入一个 TInterfaceList 中,然后我想扫描列表中的特定接口(interface)类型(例如“IMeasureTemperature”),构建另一个指向导出这些接口(interface)的对象的指针列表。我希望不对这些对象的位置做出任何假设,有些对象可能会导出不止一种类型的接口(interface)。我知道我可以使用类层次结构来做到这一点,例如:
If FList[I] is TMeasureTemperature then ..
但是我想做一些与接口(interface)类型类似的事情,这可能吗?
最佳答案
只需使用支持,如下所示:
var
oMTIntf: IMeasureTemperature;
...
If Supports(FList[I], IMeasureTemperature, oMTIntf) then ..
关于delphi - 如何在 Delphi TInterfaceList 中存储和定位多个接口(interface)类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2959163/