Hi
Use unit 'TypInfo' and maybe (when it's ready) 'Rtti'.
and then something like this:var
Cnt,Lp: ptrint;
OrdVal: ptrint;
StrName,StrVal: String;
FloatVal: Extended;
PropInfos: PPropList;
PropInfo: PPropInfo;
begin
{ Find out how many properties we'll be considering and get them }
Cnt := GetPropList(PTypeInfo(Self.ClassInfo),PropInfos);
try
for Lp:= 0 to Cnt - 1 do begin { Loop through all the selected properties }
PropInfo:= PropInfos^[Lp];
StrName:= PropInfo^.Name;
{ Check the general type of the property and read/write it in an appropriate way }
case PropInfo^.PropType^.Kind of
tkInt64,tkInteger,tkChar,tkEnumeration,tkBool,tkQWord,
tkSet,tkClass,tkWChar: begin
OrdVal:= GetOrdProp(Self,PropInfo);
Ini.WriteInt64('General',StrName,OrdVal);
end; { ordinal types }
tkFloat: begin
FloatVal:= GetFloatProp(Self,PropInfo);
Ini.WriteFloat('General',StrName,FloatVal);
end; { floating point types }
tkWString,tkLString,tkAString,
tkString: begin
{ Avoid copying 'Name' - components must have unique names }
// if UpperCase(PropInfo^.Name) = 'NAME' then Continue;
StrVal:= GetStrProp(Self,PropInfo);
Ini.WriteString('General',StrName,StrVal);
end; { string types }
else ;
end;
end;
finally
FreeMem(PropInfos,Cnt*sizeof(pointer)); { typinfo allocates like this: getmem(PropList,result*sizeof(pointer)); }
end;
Ini.UpdateFile; { persist to disk }
Result:= true;;
end;
The above example, writes the properties to an ini-file, but I think you'll get the gist of it...
HTH
Regards Benny
Thank you very much Benny. The code was way over my head right now. So I plugged it into chatGPT and asked for a breakdown. I was quite pleasantly surprised by the result. See below for what chatGPT broke down the code into. I would still like to see some code that does not use
self and does not write to a ini file but simply gets the properties then lists them in a memoedit if possible. I am includinmg the breakdown from chatGPT because I sincerely think it can help other newbies understand somewhat what the code does. I was quite frankly lost until I asked chatGPT for help
Breakdown of the Code: Variable Declarations: Cnt, Lp: ptrint;: Variables to hold counts and loop indices.
OrdVal: ptrint;: Holds integer/ordinal property values.
StrName, StrVal: String;: Hold property names and string values.
FloatVal: Extended;: Holds floating-point property values.
PropInfos: PPropList;: Pointer to an array of PPropInfo.
PropInfo: PPropInfo;: Pointer to a TPropInfo structure.
Getting Property Information: Cnt := GetPropList(PTypeInfo(Self.ClassInfo), PropInfos);: Retrieves a list of properties (
PropInfos) for the current object (
Self).
Self.ClassInfo provides type information about the class of
Self.
GetPropList fills
PropInfos with pointers to
TPropInfo structures.
Cnt holds the number of properties found.
Iterating through Properties:
for Lp := 0 to Cnt - 1 do begin ... end;: Iterates through each property in
PropInfos.
Processing Each Property: PropInfo := PropInfos^[Lp];: Gets the
TPropInfo structure for the current property.
StrName := PropInfo^.Name;: Retrieves the name of the property.
Switch Statement (case): case PropInfo^.PropType^.Kind of ... end;: Checks the type (
Kind) of the property.
Handling Different Property Types: Ordinal Types (tkInt64, tkInteger, tkChar, tkEnumeration, tkBool, tkQWord, tkSet, tkClass, tkWChar): Uses
GetOrdProp to retrieve the ordinal value (
OrdVal) and writes it to an INI file section ('
General') using
Ini.WriteInt64.
Floating Point Types (tkFloat): Uses
GetFloatProp to retrieve the floating-point value (
FloatVal) and writes it to the INI file section ('
General') using
Ini.WriteFloat. String Types (tkWString, tkLString, tkAString, tkString): Uses
GetStrProp to retrieve the string value (
StrVal) and writes it to the INI file section ('
General') using
Ini.WriteString.
Memory Management: FreeMem(PropInfos, Cnt * SizeOf(Pointer));: Frees memory allocated for
PropInfos after processing all properties.
Persistence:
Ini.UpdateFile;:
Persists changes made to the INI file (
Ini) to disk.
Return Value: Result := True;: Returns
True indicating successful processing.
Notes:
TypInfo Unit: The TypInfo unit provides functions (
GetPropList, GetOrdProp, GetFloatProp, GetStrProp) for inspecting and manipulating properties of objects at runtime.
INI File Handling: The code assumes the use of an
Ini object (presumably
TIniFile or similar) to read from and write to an INI file for persistence.
Property Types: Different types of properties (
ordinal, floating-point, string) are handled differently based on their kind (
tk*) using type information retrieved from
TPropInfo.
Error Handling: Error handling (e.g., checking for
nil values, ensuring proper property names) is not explicitly shown in this snippet and should be considered for production code.
This code snippet showcases how to dynamically read and write object properties to an INI file based on their type, demonstrating the flexibility of Object Pascal in handling runtime reflection and data persistence tasks. Adjustments can be made based on specific application requirements and property types involved.