Einzelnen Beitrag anzeigen

kompi

Registriert seit: 27. Apr 2012
Ort: Duisburg
56 Beiträge
 
Delphi 12 Athens
 
#1

Indexed Properties über RTTI abfragen

  Alt 26. Feb 2025, 15:36
Delphi-Version: 12 Athens
Ich habe ein Objekt in Delphi mit einem Index -Property. Das Property hat dazu einen Getter definiert.

Ich möchte nun diese Property mittels RTTI abfragen.

Hier der Code, den ich bisher habe

Delphi-Quellcode:
Class function Tpropertyrec.trygetpropertyfromindex(apropclass : Trttiinstancetype; anode : pointer; aproperty : string; aindex : Tvalue;out res: string): boolean;
var
    Lprop : Trttiindexedproperty;
  value : Tvalue;
begin
  lprop := apropclass.GetIndexedProperty(aproperty);
  result := false;
  if assigned(lprop) then
  begin
    Value := lprop.GetValue(anode,[0]);
    res := Value.asstring;
  end;
end;

Class function Tpropertyrec.trygetpropertyfromindex(aobject : Tobject; aproperty : string; aindex : Tvalue; out res: string): boolean;
var
   lcontext : Trtticontext;
  lclass : Trttiinstancetype;
begin
    lcontext := Trtticontext.create;
  try
       lclass := lcontext.GetType(aobject.ClassType) as Trttiinstancetype;
     result := trygetpropertyfromindex(lclass, aobject, aproperty,aindex ,res);
  finally
    lcontext.free;
  end;
end;
Ich rufe nun die untere Funktion auf, die dann die obere Funktion aufruft.
Bei der Zuweisung an Value in der oberen Funktion erhalte ich eine Zugriffsverletzung. Was mache ich falsch?

Danke an alle
Detlef Schmitz
  Mit Zitat antworten Zitat