Einzelnen Beitrag anzeigen

Benutzerbild von stahli
stahli

Registriert seit: 26. Nov 2003
Ort: Halle/Saale
4.337 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