0

The System.Rtti.TRttiType.GetProperties method returns the results as ordered by the class/interface hierarchy. This means that the most recently included properties are located at the top of the list.

What is the simplest way to get the properties in ancestral order? That is the properties from the inherited classes come first in the TArray?

For example, given this class structure:

Type 
  AncestorClass = Class(TObject)
  published 
    property AncestorProperty: Integer;
  end;

  DescendantClass = Class(AncestorClass)
  published 
    property DescendantProperty: Integer;
  end;

The GetProperties result for DescendantClass would be DescendantProperty followed by AncestorProperty.

What I would like is for the result to be AncestorProperty followed by DescendantProperty.

Looping through the array in reverse order isn't suitable, as it also reverses the order of the properties within each class.

I suppose it should be possible to look at the TRttiProperty.ClassParent to see if it has one, and if do, call GetProperties on that class type instead. By using recursion we would get the base class properties first, but I can't get it straight as to how that would look.

SiBrit
  • 1,460
  • 12
  • 39
  • "*What is the simplest way to get the properties in ancestral order?*" - The list is returned in the order that `GetProperties()` was designed to return, and there is no option to change that order. If you don't like the order, you will have to enumerate the class hierarchy yourself. If you do, use `GetDeclaredProperties()` instead so you read properties on a per-class basis and skip inherited properties. – Remy Lebeau Feb 08 '22 at 05:24
  • @RemyLebeau. Thanks for that. In the end we didn't bother as it was only being used to generate the JSON response from a REST request and it was decided it didn't need to be human readable. While we humans like it if the properties and values were in the order of importance, a third party program that is deserialising the JSON doesn't care. – SiBrit Mar 08 '22 at 03:49

0 Answers0