Thema: Delphi Rtti und Enum problem

Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#1

Rtti und Enum problem

  Alt 13. Aug 2022, 10:13
Hallo liebe Delphi Freunde, in einem kleinen Projekt von mir kann ein Anwender "unbekannte" wincontrols an meine Klasse übergeben, innerhalb meiner Klasse setze ich auch Properties des unbekannten Controls.
Dabei bin ich so vorgegangen das meine Basis-Klasse für die unbekannten dinger vom TControl abgeleitet werden.
(ich habe unter Lazarus angefangen und dort funktioniert es, wenn ich es unter Delphi ausführe erhalte ich einen Typecast Error)
Delphi-Quellcode:
function SetProperty(const AControl: TControl; const AProperty: string; const AValue: TValue): Boolean;
var
  LControl: TControl;
  LRttiContext: TRttiContext;
  LRttiProperty: TRttiProperty;
begin
  Result := False;
  LControl := AControl;
  LRttiProperty := LRttiContext.GetType(LControl.ClassType).GetProperty(AProperty);
  if ((LRttiProperty <> nil) and (LRttiProperty.Visibility in [mvPrivate, mvProtected, mvPublic, mvPublished])) then
  begin
    LRttiProperty.SetValue(LControl, AValue);
    // Result := (LRttiProperty.GetValue(Ctrl) = AValue); // Error: Operator is not overloaded: "TValue" = "TValue"
    Result := True;
  end;
end;
mit dieser Methode setze ich Felder die TControl/TWinControl nicht bereitstellt, funktioniert soweit auch ganz gut.
Delphi-Quellcode:
      {$IFDEF FPC}
      SetProperty(FControls[ii], 'Alignment', Ord(taCenter));
      SetProperty(FControls[ii], 'BorderStyle', Ord(bsSingle));
      {$ELSE FPC}
      SetProperty(FControls[ii], 'Alignment', 2); // typecast error
      SetProperty(FControls[ii], 'BorderStyle', 1); // typecast error
      {$ENDIF FPC}
Wie macht man es unter Delphi korrekt bitte?
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat