Einzelnen Beitrag anzeigen

Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.541 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Propertys auf Klasse variabel ansprechen

  Alt 19. Jul 2016, 10:55
Per RTTI geht das. Schau Dir mal die Unit System.Rtti dazu an.

[edit] Hier ein Minimalbeispiel ohne große Fehlerbehandlung:
Delphi-Quellcode:
uses System.Rtti;

function GetPropString(const Instance: TObject; const Propname: string): string;
var
  ctx: TRttiContext;
  rt: TRttiType;
  Prop: TRttiProperty;
  Value: TValue;
begin
  Result := '';
  ctx := TRttiContext.Create;
  rt := ctx.GetType(Instance.ClassType);
  if Assigned(rt) then
    begin
      Prop := rt.GetProperty(Propname);
      if Assigned(Prop) then
        begin
          Value := Prop.GetValue(Instance);
          Result := Value.ToString;
        end;
    end;
end;
[/edit]
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen

Geändert von DeddyH (19. Jul 2016 um 11:53 Uhr)
  Mit Zitat antworten Zitat