I'm on Delphi 10.3.
TTestRTTI = class
field1: integer;
end;
procedure testRTTI();
var
field : TRttiField;
fields: TArray<TRttiField>;
testInstance: TTestRTTI;
val: Integer;
begin
testInstance := TTestRTTI.Create();
testInstance.field1 := 5;
fields := TRttiContext.Create().GetType(TypeInfo(TTestRTTI)).GetFields();
for field in fields do begin
val := field.GetValue(@testInstance).AsInteger;
end;
// val is not 5 ?
OutputDebugString(PChar(val.ToString()));
end;
It seems GetValue does not return the correct information. What am I doing wrong ?
Edit : It seems that Delphi returns a Long instead of an Integer ? Is this a platform problem ? I'm on Windows 10 64 bits.
Okay so my problem with that I was passing the address of
testInstancedirectly toGetValue(). SincetestInstanceis a class, that doesn't work.Problem is, when working with generics, passing either one doesn't work. So instead of having a generic procedure like:
You need :
And then you can pass
adirectly toPByteorGetValue.