I want to use RTTI to determine if a control is read-only. My code currently looks like this and works fine.
function ControlIsReadOnly(const Control: TControl): Boolean;
var
Value: Variant;
begin
try
Value := GetPropValue(Control, 'ReadOnly');
Result := Value;
except
Result := False;
end;
end;
Is there a nice way to do this without raising an exception if the property is absent?
You can use GetPropInfo()
, which returns a pointer to a TPropInfo
if the property is found, or nil
if not found. GetPropValue()
(and other getter functions) has an overload which takes a TPropInfo
pointer, eg:
function ControlIsReadOnly(const Control: TControl): Boolean;
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(Control, 'ReadOnly');
if PropInfo <> nil then
Result := GetPropValue(Control, PropInfo)
// or:
// Result := Boolean(GetOrdProp(Control, PropInfo))
else
Result := False;
end;