Thema: Delphi Umgang mit Interfaces

Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.060 Beiträge
 
Delphi 10.4 Sydney
 
#17

AW: Umgang mit Interfaces

  Alt 10. Dez 2013, 09:03
Da man aber Supports nicht ohne eine IID_xxx aufzurufen kann, muss man hier wieder ein konkretes Interface angeben. Also etwa:

Delphi-Quellcode:
function TMyList.Get<T>(const AIndex: Integer): T;
begin
  if not Supports(FList[AIndex].fMy, IID_BASE, Result) then
    Result := nil;
end;
Das ist so nicht richtig, siehe Doku:
Delphi-Quellcode:
function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean;
function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean;
function Supports(const Instance: IInterface; const IID: TGUID): Boolean;
function Supports(const Instance: TObject; const IID: TGUID): Boolean;
function Supports(const AClass: TClass; const IID: TGUID): Boolean;
Wobei IID hier auch direkt ein Interface-Typ sein kann (IMyFunnyInterface, IInteger, IDoSomeThing).

Codebeispiel anhand deines Falls:

Delphi-Quellcode:
type
  TMyDoSomething = Class(TMyCompare, IInteger, ISomeThing)
  ...
  end;

var
  MySomeThingObject : ISomeThing;
begin
  MySomeThingObject := TMyDoSomething.Create;
  if Supports(MySomeThingObject, IInteger) then
  begin
    ShowMessage('IInteger wird voll unterstützt!!!');
  end;
end;
  Mit Zitat antworten Zitat