AGB  ·  Datenschutz  ·  Impressum  







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

[Rtti] Feld in ObjectList finden

Ein Thema von Jens01 · begonnen am 24. Okt 2011 · letzter Beitrag vom 24. Okt 2011
 
Benutzerbild von stahli
stahli

Registriert seit: 26. Nov 2003
Ort: Halle/Saale
4.352 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: [Rtti] Feld in ObjectList finden

  Alt 24. Okt 2011, 16:31
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;
Stahli
http://www.StahliSoft.de
---
"Jetzt muss ich seh´n, dass ich kein Denkfehler mach...!?" Dittsche (2004)
  Mit Zitat antworten Zitat
 


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 22:17 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-2025 by Thomas Breitkreuz