looking for help retrieving values ββfrom an entry in CodeSite. Although I have an entry with basic values ββ(int, string ..), everything works okey.
But the problem is with the arrays in the record.
Surname: array [0..35] WideChar
Name: array [0..25] WideChar
(I found this, but is there any way?)
Getting the type of the RTTI error record field for static arrays
While I want to get the value of the Surname / Name field, TRttiField.FieldType.TypeKind is null.
Does anyone know how to get property values ββin a string?
There is my procedure.
class procedure TCodeSite.SendObject<T>(Category: String; lObject : T; Msg: String);
var
lRTTIContext : TRttiContext;
lRTTIObjectType : TRttiType;
lRTTIField : TRttiField;
lRTTIRecordInRecord : TRttiRecordType;
lRTTIFieldInRecord : TRttiField;
lRTTIPointerType : TRttiPointerType;
lRTTIPointerValue : TValue;
lFieldName : string;
lFieldNameInRecord : string;
lStringList : TStringList;
begin
{$IfNDef CodeSiteDisabled}
if isActiveDebugCategory(Category) then
begin
lStringList := TStringList.Create;
lRTTIContext := TRttiContext.Create;
lRTTIObjectType := lRTTIContext.GetType(TypeInfo(T));
if lRTTIObjectType.TypeKind = tkRecord then
begin
for lRTTIField in lRTTIObjectType.GetFields do
begin
lFieldName := ParseFieldName(lRTTIField.ToString);
case lRTTIField.FieldType.TypeKind of
tkRecord:
begin
lRTTIRecordInRecord := lRTTIField.FieldType.AsRecord;
lStringList.Add(lFieldName);
for lRTTIFieldInRecord in lRTTIRecordInRecord.GetFields do
begin
lFieldNameInRecord := ParseFieldName(lRTTIFieldInRecord.ToString);
lStringList.Add(' '+lFieldNameInRecord +' '+GetFieldValue<T>(Addr(lObject), lRTTIFieldInRecord));
end;
end;
tkPointer:
begin
lRTTIPointerType := lRTTIField.FieldType as TRttiPointerType;
if lRTTIPointerType.ReferredType.TypeKind = tkRecord then
begin
lRTTIPointerValue := lRTTIField.GetValue(Addr(lObject));
if (not lRTTIPointerValue.IsEmpty) then
begin
for lRTTIFieldInRecord in lRTTIPointerType.ReferredType.GetFields do
begin
lStringList.Add(lFieldName + GetFieldValue<T>(Addr(lObject), lRTTIFieldInRecord));
end;
end;
end;
end
else lStringList.Add(lFieldName + ' ' + GetFieldValue<T>(Addr(lObject), lRTTIField));
end;
end;
CodeSite.Category := Category;
CodeSite.Send(Msg, lStringList);
end;
lRTTIContext.Free;
end;
{$EndIf}
end;
There is a second procedure for getting a value from a field
class function TCodeSite.GetFieldValue<T>(const pipInstance : Pointer;
const piclField : TRttiField) : string;
begin
if not (Assigned(piclField.FieldType)) then
begin
Result := 'Pica';
Exit;
end;
case piclField.FieldType.TypeKind of
tkEnumeration : Result := BoolToStr(piclField.GetValue(pipInstance).AsBoolean);
tkFloat: Result := FloatToStr(piclField.GetValue(pipInstance).AsExtended);
tkInt64: Result := IntToStr(piclField.GetValue(pipInstance).AsInt64);
tkInteger: Result := IntToStr(piclField.GetValue(pipInstance).AsInteger);
tkString,tkUString: Result := Trim(piclField.GetValue(pipInstance).AsString);
end;
end;
source
share