Hi Everyone.
My first post on the Lazarus forum.
I am coming from Delphi which has become so ridiculously expensive, so I'm porting to Lazarus. Thanks to the Lazarus community for making such a great open source project.
I want to iterate through a class's fields, methods and properties to get and set various information. This has to be generic for any class, so it needs to discover what fields, methods and properties are present. In Delphi I can use the code below (see
https://stackoverflow.com/questions/4466629/iterate-member-variables)
Excuse my ignorance. In Lazarus/FPC I can not find any rtti unit. How to do this in Lazarus/FPC?
Many thanks
Martin
{******************************************}
uses rtti;
///
procedure GetInfo(obj: TObject);
var
context: TRttiContext;
rType: TRttiType;
field: TRttiField;
method: TRttiMethod;
prop: TRttiProperty;
SL: TStringlist;
begin
context := TRttiContext.Create;
rType := context.GetType(obj.ClassType);
SL := TStringlist.create;
for field in rType.GetFields do
If field <> nil then
SL.Add('Field name = ' + Field.Name);
;//do something here
for method in rType.GetMethods do
If method <> nil then
SL.Add('Method name = ' + method.Name);
;//do something here
for prop in rType.GetProperties do
;//do something here
If prop <> nil then
SL.Add('Property name = ' + prop.Name);
SL.SaveToFile('C:...[whateverpath]...\PTester.txt');
SL.Free;
end;