What is the simplest way to get the RTTI properties in ancestral order?

103 views Asked by At

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.

0

There are 0 answers