Recent

Author Topic: Enumerating properties and methods of a class at runtime with RTTI  (Read 316 times)

simsee

  • Full Member
  • ***
  • Posts: 235
I need to enumerate the properties and methods of a class at runtime. Is this already possible with classic RTTI, or do I need the extended RTTI, which is still under development? Thank you.

cdbc

  • Hero Member
  • *****
  • Posts: 2685
    • http://www.cdbc.dk
Re: Enumerating properties and methods of a class at runtime with RTTI
« Reply #1 on: November 23, 2025, 08:39:02 pm »
Hi
If the class is compiled with {$M+} __and__ the properties and methods are Published, then yes, you can enumerate them with Classic RTTI.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

jamie

  • Hero Member
  • *****
  • Posts: 7602
Re: Enumerating properties and methods of a class at runtime with RTTI
« Reply #2 on: November 23, 2025, 11:45:02 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,TypInfo;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. Var
  35.   TheList:PPropList=Nil;
  36.   I,J : Longint;
  37.   S:string;
  38. begin
  39.  Memo1.Clear;
  40.  J:= GetPropList(Form1, TheList);
  41.  For I:=0 to J-1 do
  42.     begin
  43.     With TheList^[i]^ do
  44.       begin
  45.       WriteStr(S,'Property ',i+1:3,': ',name:30);
  46.       Memo1.Lines.Add(S);
  47.       writeStr(S,'  Type: ',typinfo.PropType(form1,Name));
  48.       Memo1.Lines.Add(S);
  49.       end;
  50.     end;
  51.  Freemem(TheList);
  52. end;
  53.  
  54. end.
  55.  
  56.  

This works using FPC 3.2.2

Jamie
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018