Search code examples
delphidelphi-xertti

How can I get Record's details using Rtti?


I'm coding a Rtti class, intended to simplify and generalize operations with Rtti:

tRTTI_Assistant = class (tObject)
 private
  fSourceObject : tObject;
 ...
  property SourceObject : tObject read fSourceObject write fSourceObject;
 ...
 end;

How can I declare a property that will receive a Record?


Solution

  • You cannot add a record class property that can take any record structure and use RTTI to resolve the inner details of the record.

    For this to work you have to use another way. A TValue can be used with RTTI to resolve any type.

    So declare:

    tRTTI_Assistant = class (tObject)
     private
      //fSourceObject : tObject; // ! Use AnySource for objects as well
      fAnySource : TValue;
     ...
      //property SourceObject : tObject read fSourceObject write fSourceObject;
      property AnySource: TValue read fAnySource write fAnySource;
     ...
     end;
    

    And call it:

    myAssistant.AnySource := TValue.From(myRecord);
    

    Now you can use RTTI for resolving not just record types, but any type.

    See Convert Record to Serialized Form Data for sending via HTTP for examples how work with RTTI on a TValue: