I have the following program.
procedure TForm1.Button1Click(Sender: TObject);
begin
var con: TRttiContext;
var meth := con.GetType(TButton).GetMethod('Click');
meth.Invoke(BitBtn1, []);
end;
BitBtn1
is a TBitBtn. As you can see, meth
is a method object got from type TButton
. But, by my testing, it can be invoked against a TBitBtn without any issue. Is this expected?
CodePudding user response:
The RTTI you've posted works because both TBitBtn
and TButton
share a common ancestor (TCustomButton
) which implements the Click
method.
The code would fail if you used two types that didn't descend from an ancestor that implemented the same method, such as TEdit
and TMemo
. Both allow you to enter text, but TMemo
has the property Lines
. TEdit
does not, which would cause the code you've posted to fail.