The question here shows a way to create a compatible TValue for use with SetValue. I am trying to do a general version of this to use RTTI to store a class in an INI file. This is my short code:
procedure TMyClass.LoadRTTI(xObject: TObject);
var
LContext: TRttiContext;
LClass: TRttiInstanceType;
xField : TRttiField;
szNewValue : String;
xValue : TValue;
begin
LContext := TRttiContext.Create;
LClass := LContext.GetType(xObject.ClassType) as TRttiInstanceType;
for xField in LClass.GetDeclaredFields do
begin
szNewValue := IniFile.ReadString(szSection, xField.Name, '');
if szNewValue <> '' then
begin
case xField.FieldType.TypeKind of
tkEnumeration: xValue := StrToIntDef(szNewValue, xField.GetValue(xObject).AsOrdinal);
end;
xField.SetValue(xObject, xValue);
end;
end;
end;
In response to the answer, the solution was to get the value using the TValue.From () method, but this requires a variable of the appropriate type. I don't have a type like my code doesn't know what it is.
I am looking for an example of a general way to get a value in a string from RTTI and bring it back again. I have not yet found a good tutorial that covers this.
source
share