Registriert seit: 27. Nov 2017
2.490 Beiträge
Delphi 7 Professional
|
AW: Wie *schnell* auf Properties anhand eines String-Bezeichners zugreifen?
8. Jul 2019, 14:16
Delphi-Quellcode:
uses
..., TypInfo, ...
function HasProperty(AClass : TObject; APropertyName : String) : Boolean;
begin
Result := Assigned(GetPropInfo(AClass.ClassInfo, APropertyName));
end;
...
if HasProperty(ADataObj, ' ValueA') then begin
try
if GetStrProp(ADataObj, ' ValueA') = sName then begin
SetStrProp(ADataObj, ' ValueA', ' ');
end else
// Analog zu Get- / SetStrProp gibt es weitere Routinen, die Du typabhängig nutzen kannst.
...
except
on e : Exception do MessageDlg(e. Message,mtErrr,[mbOk],0);
end;
end;
...
Daraus könnte sowas (nur hingedaddelt) werden:
Delphi-Quellcode:
function HasProperty(AClass : TObject; APropertyName : String) : Boolean;
begin
Result := Assigned(GetPropInfo(AClass.ClassInfo, APropertyName));
end;
procedure GetFieldValue(ADataObj: TMyDataObj; const FieldName: string): string;
begin
if HasProperty(ADataObj, Fieldname) then result := GetStrProp(ADataObj, FieldName) else result := '?';
end;
... oder ...
procedure GetFieldValue(ADataObj: TMyDataObj; const FieldName: string): string;
begin
Result := ifThen(HasProperty(ADataObj, Fieldname),GetStrProp(ADataObj, FieldName),'');
end;
... oder ...
procedure GetFieldValue(ADataObj: TMyDataObj; const FieldName: string): string;
begin
Result := ifThen(Assigned(GetPropInfo(ADataObj.ClassInfo, Fieldname)),GetStrProp(ADataObj, FieldName),'');
end;
|
|
Zitat
|