Rtti for Variant Records

I am trying to write some kind of object / record serializer with Delphi 2010 and wonder if there is a way to detect if the record is a recording variant. For instance. TRect entry, as defined in Types.pas:

TRect = record
case Integer of
  0: (Left, Top, Right, Bottom: Longint);
  1: (TopLeft, BottomRight: TPoint);
end; 

Since my serializer needs to work recursively in my data structures, it will go down to TPoint records and generate redundant information in my serialized file. Is there a way to avoid this by getting detailed information about the recording?

+3
source share
1 answer

One solution may be as follows:

procedure SerializeRecord (RttiRecord : TRttiRecord)

var
  AField : TRttiField;
  Offset : Integer;

begin
Offset := 0;
for AField in RttiRecord.Fields do
  begin
  if AField.Offset < Offset then Exit;
  Offset := AField.Offset; //store last offset
  SerializeField (AField);
  end;
end;

. , . - ( wikipedia.org):

type   
  TVarRec = packed record
  case Byte of
    0: (FByte: Byte;
        FDouble: Double);
    1: (FStr: ShortString);
  end;

FByte=6
FDouble=1.81630607010916E-0310

FStr=Hello!

, , , , .

, , - , , .

+1

Source: https://habr.com/ru/post/1743788/


All Articles