trtti4.pp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. {$IFDEF FPC}
  2. {$mode objfpc}{$H+}
  3. {$ELSE}
  4. {$APPTYPE CONSOLE}
  5. {$ENDIF}
  6. uses
  7. SysUtils,
  8. TypInfo,
  9. Classes;
  10. type
  11. TAObject = class(TPersistent)
  12. private
  13. FCaption: string;
  14. published
  15. property Caption: string read FCaption write FCaption;
  16. end;
  17. TBObject = class(TAObject)
  18. private
  19. FIntProp: Integer;
  20. FStrProp: String;
  21. published
  22. property IntProp: Integer read FIntProp write FIntProp;
  23. property StrProp: String read FStrProp write FStrProp;
  24. end;
  25. TCObject = class(TBObject)
  26. private
  27. FAStrProp: String;
  28. published
  29. property AnotherStrProp: String read FAStrProp write FAStrProp;
  30. end;
  31. procedure ShowProperties;
  32. var
  33. Obj: TCObject;
  34. i: Longint;
  35. lPropFilter: TTypeKinds;
  36. lCount: Longint;
  37. lSize: Integer;
  38. lList: PPropList;
  39. begin
  40. Obj := TCObject.Create;
  41. lPropFilter := [tkInteger, tkLString {$ifdef FPC}, tkAString{$endif}];
  42. lCount := GetPropList(Obj.ClassInfo, lPropFilter, nil, false);
  43. lSize := lCount * SizeOf(Pointer);
  44. GetMem(lList, lSize);
  45. Writeln('Total property Count: ' + IntToStr(lCount));
  46. lCount := GetPropList(Obj.ClassInfo, lPropFilter, lList, false);
  47. for i := 0 to lCount-1 do
  48. begin
  49. Writeln('Property '+IntToStr(i+1)+': ' + lList^[i]^.Name);
  50. end;
  51. if (lList^[0]^.Name<>'Caption') then
  52. halt(1);
  53. FreeMem(lList);
  54. Obj.Free;
  55. Writeln('---------------');
  56. end;
  57. begin
  58. ShowProperties;
  59. end.