Re: How to discover a class's TypInfo fields, methods and properties lazarus / 64-FPC development ---------------------------------------------------------------------- MSGEndoDoc · 2020-07-26 12:12 Hi Re:  Re: How to discover a class's TypInfo fields, methods and properties I am trying to iterate through a set of my Pascal classes, and want to automate getting and setting of some of the class's fields, and properties.   I have worked out how to do this with class data type info similar to these guys  https://stackoverflow.com/questions/10188459/how-to-loop-all- properties-in-a-class https://forum.lazarus.freepascal.org/index.php/topic,48976.0.html and in Lazarus 2.06/FPC 3.0.4 can access the class properties no problem like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  uses TypInfo // procedure GetInfo(obj: TObject);var  a: integer; ts1: string; SL:      TStringlist;  PropCount: integer; PropList:  PPropList; PropInfo:   PPropInfo; begin   PropCount := GetPropList(obj, PropList);   // THIS IS WORKING - IDENTIIFES ALL THE PROPERTIES IN THE CLASS, BUT NOT FIELDS OR METHODS  SL := TStringlist.create;  For a := 0 to PropCount -1 do  begin;    PropInfo := PropList^[a];    SL.add('No Properties  = ' + PropCount.ToString + ', property name  = ' + PropInfo^.Name + ', Property data type  = ' + PropInfo^.PropType^.Name + ', Property value = ' + VarToStr(GetPropValue(Obj,PropInfo)));  end;  Showmessage(SL.text);  FreeAndNil(SL);  .  But I also want to access the fields, even in the class's  private section. In Delphi this can be done with the RTTI unit like this.  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   ///////////////// uses RTTI ///// procedure GetInfo(obj: TObject);var  context: TRttiContext;     // This is the object storing all the type info for a class or other data  DataList:  TRttiType;      // This holds the data record for all the class's specific fields, methods or properties  field:        TRttiField;         // This holds the data type* record for a field in the class  method:  TRttiMethod;   // This holds the data type* record for a method in the class  prop:       TRttiProperty;   // This holds the data type* record for a property in the class   {*  TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,    tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString,    tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray, tkUString,    tkClassRef, tkPointer, tkProcedure, tkMRecord);}   SL:      TStringlist;     // Were using this TStringlist to write information  to screeen. begin  context := TRttiContext.Create;  DataList := context.GetType(obj.ClassType);  SL := TStringlist.create; // Fields  for field in DataList.GetFields do  If field <> nil then    SL.Add('Field name = ' + Field.Name + ', Field Type = ' + Field.FieldType.Name + ' FieldValue = ' + Field.GetValue(obj).ToString + ' '  ); // Methods - are the procedures and functions Actually not so interesteds in methods as want the data not the methods  for method in DataList.GetMethods do     If method <> nil then     SL.Add('Method name = ' + Method.Name); // Properties  for prop in DataList.GetProperties do     If prop <> nil then     SL.Add('Property name = ' + Prop.Name + ', Property Type = ' + Prop.PropertyType.ToString + ' Property Value = ' + Prop.GetValue(obj).ToString + ' '  );   Showmessage(SL.text);  FreeAndNil(SL);  // FreeAndNil(context); // For some reason TRTTIContext does not need to be freed despite being created above - its freed by other background meansend;  In FPC 3.0.4 we only have the TypInfo unit, but I read that a RTTI unit is on its way in FPC 3.2.0 https://foundation.freepascal.org/projects/project-1 Can anyone confirm when the RTTI unit will become part of the standard Lazarus release and also if it will have the functionality like Delphi to access a class's fields, even the private ones.  As you might guess, I'm currently coding in Delphi but moving to Lazarus/FPC and so want to make sure there my code is still useful when I get onto the open (source) road ahead  Cheers, Martin ---------------------------------------------------------------------- marcov · 2020-07-26 12:26 Only in Delphi XE2+ afaik this is possible. FPC doesn't implement this yet, and only generates RTTI for published properties ---------------------------------------------------------------------- PascalDragon · 2020-07-26 21:13 Quote from: MSGEndoDoc on July 26, 2020, 02:12:16 pmCan anyone confirm when the RTTI unit will become part of the standard Lazarus release and also if it will have the functionality like Delphi to access a class's fields, even the private ones.  The Rtti unit is part of 3.2, however it's still considered experimental and work in progress and currently only supports access to published properties of classes as this requires the compiler to generate additional RTTI data. ---------------------------------------------------------------------- MSGEndoDoc · 2020-07-27 06:12 OK  Thanks for the heads up.  Look forward to v 3.2.0 and hope that the RTTI unit can be extended to also access the class fields too.  Cheers, Martin ---------------------------------------------------------------------- PascalDragon · 2020-07-27 07:10 Quote from: MSGEndoDoc on July 27, 2020, 08:12:13 amOK  Thanks for the heads up.  Look forward to v 3.2.0 and hope that the RTTI unit can be extended to also access the class fields too.  FPC 3.2.0 has already been released, so you can use it already. The Extended RTTI will probably only arrive with 3.4 at the earliest, so in roughly two years or so... ---------------------------------------------------------------------- Zaher · 2022-03-18 18:49 Quote from: PascalDragon on July 27, 2020, 09:10:25 am or so... or so ---------------------------------------------------------------------- PascalDragon · 2022-03-19 15:49 Quote from: Zaher on March 18, 2022, 07:49:35 pmQuote from: PascalDragon on July 27, 2020, 09:10:25 am or so... or so There is already a merge request for this, I “just” need to find the time to do the final review and ensure that it doesn't break any of the more problematic targets. ---------------------------------------------------------------------- Zaher · 2022-03-19 16:20 Thank you , I have a opensource project for scripting using Lua and PascalScript i want to make auto register classes and methods of internal classes, it is hard to do it twice for 2 languages what if i want to add more.