Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   [Rtti] Feld in ObjectList finden (https://www.delphipraxis.net/163984-%5Brtti%5D-feld-objectlist-finden.html)

Jens01 24. Okt 2011 15:24

Delphi-Version: 5

[Rtti] Feld in ObjectList finden
 
XE2:
Ich habe mich zwar in Rtti schon eingearbeitet, aber folgendes Problem macht mir noch etwas Sorge.
Ich suche ein Feld in einem Objekt, das in einer Liste liegt :
Delphi-Quellcode:
TAuto = class
  ..
  druck: Double;
end;

TParkplatz = class
  ..
  Auto: TObjectList<TAuto>;
end;

function WertFinden(Wert1, Wert2: string; i: Integer): Double;
var
  C: TRttiContext;
  T: RttiType;
  F1, F2: TRttiField;
begin
  T := C.GetType(TParkplatz);
  F1 := T.GetField(Wert1);
  // so und jetzt weiß ich nicht mehr weiter
end;
Aufruf:
Delphi-Quellcode:
Reifendruck_AutoNr5 := WertFinden('Auto', 'druck', 5);
Kann mir jemand auf die Sprünge helfen, wie ich an den Wert von dem Feld "Druck" kommen?

Gruss Jens

Uwe Raabe 24. Okt 2011 15:35

AW: [Rtti] Feld in ObjectList finden
 
Enn du den Wert eines Feldes haben willst, brauchst du erstmal eine Instanz der Klasse. Da diese Instanz in der Liste "Auto" der Klasse TParkplatz liegt, brauchst du erstmal eine Instanz von TParkplatz. Also muss WertFinden entweder eine Instanz von TParkplatz kennen oder du gibst der Methode eine mit.

Aber warum machst du das überhaupt mit RTTI und greifst nicht direkt auf die Eigenschaften zu?

Jens01 24. Okt 2011 15:46

AW: [Rtti] Feld in ObjectList finden
 
Zitat:

Aber warum machst du das überhaupt mit RTTI und greifst nicht direkt auf die Eigenschaften zu?
Das ist nur ein verständliches Beispiel fürs Forum. In meinem Projekt ist die Sache etwas umfangreicher. Ich suche einzelne Ergebnisse aus Ergebnislisten raus. Also dies TParkplatz ist noch mal in einer Liste drin und es verschiedene Sorten (Ableitungen) von TParkplatz. Und bei mir geht es gar nicht um Autos;)

stahli 24. Okt 2011 16:31

AW: [Rtti] Feld in ObjectList finden
 
Hmm, Dein Beispiel bzw. die Problembeschreibung finde ich etwas unübersichtlich.

Vielleicht hilft Dir mein Beispiel, wie ich Propertys meiner Objekte auslese (die mit einem Attribut markiert sein) irgendwie weiter...


Delphi-Quellcode:
function TodProp.GetPropValue(const od: Tod; PropName: String): String;
var
  PropValue: String;
  Context: TRttiContext;
  RttiType: TRttiType;
  PropInfo: TRttiProperty;
  F: Boolean;
  Attr: TCustomAttribute;
  Value: TValue;
  O: TObject;
  _od: Tod;
  _PropName: String;
begin
  Result := '';

  if (not Assigned(od)) or (PropName = '') then
    Exit;

  if Lowercase(PropName) = Lowercase('odId') then
    Exit(od.odId);

  _od := od;
  _PropName := PropName;
  CorrectSubOd(_od, _PropName); // Unterobjekte suchen
  if not Assigned(_od) then
    Exit;

  Context := TRttiContext.Create;
  RttiType := Context.GetType(_od.ClassType);

  if Assigned(RttiType) then
  begin
    for PropInfo in RttiType.GetProperties do
    begin
      if PropInfo.Name <> _PropName then
        Continue;
      F := False;
      for Attr in PropInfo.GetAttributes do
      begin
        if Attr is AttrOd then
          F := True;
      end;
      if F then
      begin
        PropValue := '';
        Value := PropInfo.GetValue(_od);
        case Value.Kind of
          tkUnknown:
            ;
          tkInteger:
            if Value.AsInteger = 0 then
              PropValue := ''
            else
              PropValue := IntToStr(Value.AsInteger);
          tkChar:
            ;
          tkEnumeration:
            if Value.AsOrdinal = 0 then
              PropValue := ''
            else
              PropValue := GetEnumName(Value.TypeInfo, Value.AsOrdinal);
          tkFloat:
            if Value.AsExtended = 0 then
              PropValue := ''
            else if Value.IsType<TDateTime> then
              PropValue := DateTimeToStr(Value.AsExtended)
            else if Value.IsType<TTime> then
              PropValue := TimeToStr(Value.AsExtended)
            else
              PropValue := FloatToStr(Value.AsExtended);
          tkString:
            PropValue := Value.AsString;
          tkSet:
            ;
          tkClass:
            begin
              O := Value.AsObject;
              if (O <> nil) and (O is Tod) then
                PropValue := (O as Tod).odId;
            end;
          tkMethod:
            ;
          tkWChar:
            ;
          tkLString:
            ;
          tkWString:
            ;
          tkVariant:
            ;
          tkArray:
            ;
          tkRecord:
            ;
          tkInterface:
            ;
          tkInt64:
            ;
          tkDynArray:
            ;
          tkUString:
            PropValue := Value.AsString;
          tkClassRef:
            ;
          tkPointer:
            ;
          tkProcedure:
            ;
        end;
        if PropValue <> '' then
          Result := PropValue;
      end;
    end;
  end;

  Context.Free;
end;

Jens01 24. Okt 2011 16:47

AW: [Rtti] Feld in ObjectList finden
 
Ich glaube, ich mache da einen gedanklichen Fehler.
In der ObjectList kann ich so direkt ja nichts mit Rtti finden. Ich muß wohl die Liste mit einer Schleife durchlaufen und dort die einzelen Objecte durchsuchen. :roll:

Jens01 24. Okt 2011 17:12

AW: [Rtti] Feld in ObjectList finden
 
Es ist nicht ganz optimal, aber jetzt habe ich das so gelöst :
Delphi-Quellcode:
Parkplatz: TParkplatz;
function WertFinden(Wert1, Wert2: string; i: Integer): Double;
var
  C: TRttiContext;
  T: RttiType;
  F1, F2: TRttiField;
  L: TList;
begin
  T := C.GetType(TParkplatz);
  F1 := T.GetField(Wert1);
  if Assigned(F1) then
  begin
    L := TList(F1.GetValue(Parkplatz).AsObject);
    T := C.GetType( TAuto );
    F2 := T.GetField(Wert2);
    if Assigned(F2) then
      Result := F2.GetValue(L[i]).AsExtended;
  end;
end;

himitsu 24. Okt 2011 17:37

AW: [Rtti] Feld in ObjectList finden
 
Statt
Delphi-Quellcode:
TList(F1.GetValue(Parkplatz).AsObject)
besser
Delphi-Quellcode:
(F1.GetValue(Parkplatz).AsObject as TList)
, denn wenn das zufällig mal keine TList sein sollte, suchst du dich eventuell noch dusskig, nach diesem Fehler.

oder auch in etwa so: (also den Typ der TList prüfen)
Delphi-Quellcode:
var L: TObject;

  T := C.GetType(TParkplatz);
  F1 := T.GetField(Wert1);
  if Assigned(F1) then
  begin
    L := F1.GetValue(Parkplatz).AsObject;
    T := C.GetType(TAuto);
    F2 := T.GetField(Wert2);
    if (L is TList) and Assigned(F2) then
      Result := F2.GetValue(TList(L)[i]).AsExtended;
  end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:23 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz