I have a number of record structures in Delphi (Berlin) which I am trying to recursively iterate through using RTTI. The code is not working for inner records. What am I doing wrong here?
Procedure WriteFields(Const RType : TRttiType;
Const Test : TTestRecord;
Var Offset : integer);
var
RFields : TArray<TRTTIField>;
i : integer;
Val : TValue;
begin
RFields := GetFields(Rtype);
try
for i := Low(RFields) to High(RFields) do
begin
if RFields[i].FieldType.TypeKind <> tkRecord then
begin
Val := rfields[i].GetValue(@Test);
writeln(Format('Field Name: %s, Type: %s, Value: %s, Offset: %d',[
RFields[i].Name,
RFields[i].FieldType.ToString,
Val.ToString,
RFields[i].Offset]));
end
else
begin
WriteLn(Format('------- Inner record : %s',[RFields[i].name]));
//recursively call this routine for the other records, and fields
Writefields(RFields[i].FieldType,Test,Offset);
end;
Offset := OffSet + RFields[i].Offset;
end;
finally
SetLength(RFIelds,0);
end;
end;
Here is my test record structure
TInfo = packed record
Age : integer;
end;
TTestRecord = packed record
Name : String;
Text : String;
Info : TInfo; //inner record structure
end;
Here's my test record data
//set a few values on it
Test.Name := 'Fred';
Test.text := 'Some random text';
Test.Info.Age := 50;
Here's the output of the code running in a console app
Size of 12
Field Name: Name, Type: string, Value: Fred, Offset: 0
Field Name: Text, Type: string, Value: Some text, Offset: 4
------- Inner record : Info
Field Name: Age, Type: Integer, Value: 38642604, Offset: 0
Total offset of bytes read 12
As you can see, the value returned for the inner record Age, is garbage.