Registriert seit: 26. Nov 2003
Ort: Halle/Saale
4.343 Beiträge
Delphi 11 Alexandria
|
AW: RTTI und Enum
5. Feb 2014, 14:40
Hier meine Funktion:
Delphi-Quellcode:
function GetPropValue(const od: TObject; PropName: String): String;
var
PropValue: String;
Context: TRttiContext;
RttiType: TRttiType;
PropInfo: TRttiProperty;
F: Boolean;
Attr: TCustomAttribute;
Value: TValue;
_od: TObject;
_PropName: String;
begin
...
Result := '';
...
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 := True;
if od is TssObject then // Attribut prüfen - kann man weg lassen
begin
F := False;
for Attr in PropInfo.GetAttributes do
begin
if Attr is AttrSsf then
begin
F := True;
Break;
end;
end;
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:
PropValue := GetEnumName(Value.TypeInfo, Value.AsOrdinal);
tkFloat:
if Value.AsExtended = 0 then
PropValue := ''
else
PropValue := FloatToStr(Value.AsExtended);
tkString:
PropValue := Value.AsString;
tkSet:
;
tkClass:
begin
if (Value.AsObject is TssObject) then
PropValue := (Value.AsObject as TssObject).Id;
end;
tkMethod:
;
tkWChar:
;
tkLString:
;
tkWString:
;
tkVariant:
;
tkArray:
;
tkRecord:
;
tkInterface:
;
tkInt64:
;
tkDynArray:
;
tkUString:
PropValue := Value.AsString;
tkClassRef:
;
tkPointer:
;
tkProcedure:
;
end;
if PropValue <> '' then
Result := PropValue; // Ergebnis
end;
end;
end;
Context.Free;
end;
|