Meine Lösung:
Delphi-Quellcode:
ISimpleInterface = interface
function GetID: Integer;
procedure SetID(AValue: Integer);
property ID: Integer read GetID write SetID;
end;
IModule = interface(ISimpleInterface)
function GetModNo: Integer;
procedure SetModNo(AValue: Integer);
property ModNo: Integer read GetModNo write SetModNo;
end;
die Liste von diesen Dingern:
Delphi-Quellcode:
TSimpleIntfList = class(TList<ISimpleInterface>)
public
function SimpleItemByID(AiID: Integer): ISimpleInterface;
end;
eine generische Liste von (beinahe) beliebigem Nachfahrtyp:
Delphi-Quellcode:
TCustomIntfList<T: ISimpleInterface> = class(TSimpleIntfList)
strict private
FrGuid: TGUID;
public
constructor Create;
public
function TypedItemAtIndex(AiIndex: Integer): T;
end;
uses
System.SysUtils
, System.TypInfo
;
constructor TNntCustomIntfList<T>.Create;
begin
inherited Create;
FrGuid := GetTypeData(TypeInfo(T))^.Guid;
end;
function TCustomIntfList<T>.TypedItemAtIndex(AiIndex: Integer): T;
var
LoItem: ISimpleInterface;
begin
LoItem := Items[AiIndex];
Supports(LoItem, FrGuid, Result);
end;
Eine konkrete Nachfahrliste:
Delphi-Quellcode:
TModuleList = class(TCustomIntfList<IModule>)
function ModuleByModNo(AiModNo: Integer): IModule;
end;
function ModuleByModNo(AiModNo: Integer): IModule;
var
LoItem : ISimpleInterface;
LoModule: IModule;
begin
for LoItem in Self do begin
if Supports(LoItem, IModule, LoModule) then begin
if LoModule.ModNo = AiModNo then begin
Exit(LoModule);
end;
end;
end;
Result := nil;
end;