Einzelnen Beitrag anzeigen

Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.014 Beiträge
 
Delphi 12 Athens
 
#7

AW: Supports(..) liefert Referenz welche AV auslöst

  Alt 20. Jan 2015, 08:56
Hier mal das Originalbeispiel etwas erweitert. Man vergleiche die Reihenfolge der Methodenaufrufe im Source mit der der tatsächlich ausgeführten Methoden.

Delphi-Quellcode:
program Project4;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  System.Classes;

type
  IMyInterface = interface
    ['{88DDD9A5-F4BC-47B9-9240-31B1C986F230}']
    procedure doSomething();
  end;

  IMyInterface2 = interface
    ['{D9B8FC6D-2F71-4E31-A5E7-0CCA98146E78}']
    procedure doSomeotherthing();
  end;

  TMyClass = class(TInterfacedPersistent, IMyInterface, IMyInterface2)
  public
    procedure doSomething();
    procedure doSomeotherthing();
  end;

procedure justSupportsThings();
var
  myObject: TObject;
  myIntf: IMyInterface;
  myIntf2: IMyInterface2;
begin
  myObject := TMyClass.Create();
  try
    if Supports(myObject, IMyInterface, myIntf) then
      myIntf.doSomething();

    if Supports(myObject, IMyInterface2, myIntf2) then
      myIntf2.doSomeotherthing();

    if Supports(myObject, IMyInterface2, myIntf) then
      myIntf.doSomething();

    if Supports(myObject, IMyInterface, myIntf2) then
      myIntf2.doSomeotherthing();

  finally
    myObject.Free;
  end;
end;

procedure TMyClass.doSomeotherthing;
begin
  Writeln('Someotherthing');
end;

procedure TMyClass.doSomething;
begin
  Writeln('Something');
end;

begin
  try
    justSupportsThings();
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  readln;

end.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat