Einzelnen Beitrag anzeigen

Benutzerbild von ConnorMcLeod
ConnorMcLeod

Registriert seit: 13. Okt 2010
Ort: Bayern
490 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: TList / Interfaces

  Alt 20. Jan 2023, 04:23
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;
Nr.1 Delphi-Tool: [F7]

Geändert von ConnorMcLeod (20. Jan 2023 um 05:30 Uhr)
  Mit Zitat antworten Zitat