AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Indexed Properties über RTTI abfragen

Offene Frage von "mytbo"
Ein Thema von kompi · begonnen am 26. Feb 2025 · letzter Beitrag vom 26. Feb 2025
Antwort Antwort
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
mytbo

Registriert seit: 8. Jan 2007
478 Beiträge
 
#2

AW: Indexed Properties über RTTI abfragen

  Alt 26. Feb 2025, 20:55
Bei der Zuweisung an Value in der oberen Funktion erhalte ich eine Zugriffsverletzung. Was mache ich falsch?
Delphi-Quellcode:
var
  strList: TStringList;
begin
  strList := TStringList.Create;
  try
    strList.Add('Peter');
    strList.Add('Klaus');
    strList.Add('Petra');

    var rttiType: TRttiType := TRttiContext.Create.GetType(TypeInfo(TStringList));
    if rttiType <> Nil then
    begin
      var rttiProp: TRttiIndexedProperty := rttiType.GetIndexedProperty('Strings');
      if (rttiProp <> Nil)
        and (rttiProp.PropertyType.TypeKind = tkUString) then
      begin
        var value: TValue := rttiProp.GetValue(strList, [1]);
        ShowMessage(value.AsString);
      end;
    end;
  finally
    strList.Free;
  end;
In diesem Fall zum Beispiel, wenn die Liste leer wäre.

Bis bald...
Thomas
  Mit Zitat antworten Zitat
Antwort Antwort

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:53 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz