I am on Delphi 2010 and I found a couple of problems with your code. First of all, I could not get the RTTI methods to work with the built-in declaration of a character array. I changed it to:
type
TCharArray = array[0..31] of WideChar;
TRec = record
id:dWord;
name:TCharArray;
end;
If you declare an inline array as you did, the call GetValue
calls AV. This is probably fixed in XE or maybe I'm using RTTI incorrectly.
Secondly, you need special handling of arrays as opposed to scalar values:
procedure Main;
var
i, j: Integer;
rec: TRec;
rttiContext: TRttiContext;
rttiType: TRttiType;
fields: TArray<TRttiField>;
val: TValue;
s: string;
begin
rec.id := 1;
rec.name := 'Hello Stack Overflow';
rttiType := rttiContext.GetType(TypeInfo(TRec));
fields := rttiType.GetFields;
for i := low(fields) to high(fields) do begin
val := fields[i].GetValue(@rec);
if val.IsArray then begin
s := '';
for j := 0 to val.GetArrayLength-1 do begin
s := s+val.GetArrayElement(j).ToString;
end;
Writeln(s);
end else begin
Writeln(val.ToString);
end;
end;
end;
Conclusion:
1
Hello Stack Overflow
This is obviously not production code, but it should at least get you back on the road!
P.S. , RTTI. !