Fajela Tajkiya
Fajela Tajkiya

Reputation: 694

Rtti method got from Type1 can be invoked on object of Type2

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?

Upvotes: 0

Views: 110

Answers (1)

Ken White
Ken White

Reputation: 125757

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.

Upvotes: 3

Related Questions