In the debate about the closing of the class helper loophole that allowed easy access to private members (fields and methods) of a class in Delphi 10.1 Berlin, it is often claimed that
- extended RTTI allows access to the private members of a class that was compiled with (extended) RTTI enabled, and
- all VCL/RTL/FMX classes have this enabled.
However, if I run this simple unit (a simple form with one TListBox, nothing else):
unit RttiAccessTest;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Rtti, Vcl.StdCtrls;
type
TForm16 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form16: TForm16;
implementation
{$R *.dfm}
function GetMethodString(const MethodName: string): string;
var
M: TRTTIMethod;
I: Integer;
begin
M := TRttiContext.Create.GetType(TCustomForm).GetMethod(MethodName);
if Assigned(M) then
Result := 'Method ' + MethodName + ': ''' + M.ToString + ';'''
else
Result := 'Method ' + MethodName + ' cannot be found';
end;
procedure TForm16.FormCreate(Sender: TObject);
begin
Listbox1.Items.Add(GetMethodString('SetWindowState'));
Listbox1.Items.Add(GetMethodString('ShowModal'));
end;
end.
The text in the list box is:
Method SetWindowState cannot be found
Method ShowModal: 'function ShowModal: Integer;'
This means I cannot get access to this private method SetWindowState
of TCustomForm
. Is this because not all classes in the RTL/VCL/FMX have extended RTTI, or am I doing something wrong?
If I am doing something wrong or forgetting something, then what? In other words, what do I have to do to get RTTI access to, say, SetWindowState
of TCustomForm
? I cannot get this access in Seattle or earlier either.
Note
I know how to get access to the method anyway, using the fact that class helpers can still get the address of private methods, but that is not my question. I am particularly asking about how to do this with RTTI.
METHODINFO
was not enough, see my answer. – LU RD