Discovering the class where a property is first published with multiple levels of inheritance

delphi, enumeration, properties, rtti

Solution

This works for me. The crux is getting the parent's TypeInfo from the passed through child TypeInfo

procedure InheritanceLevel(AClassInfo: PTypeInfo; const AProperty: string; var level: Integer);
var
  propInfo: PPropInfo;
  propCount: Integer;
  propList: PPropList;
  ix: Integer;
begin
  if not Assigned(AClassInfo) then Exit;
  propCount := GetPropList(AClassInfo, propList);
  for ix := 0 to propCount - 1 do
  begin
    propInfo := propList^[ix];
    if propInfo^.Name = AProperty then
    begin
      Inc(level);
      InheritanceLevel(GetTypeData(AClassInfo).ParentInfo^, AProperty, level)
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  level: Integer;
begin
  level := 0;
  InheritanceLevel(PTypeInfo(TForm.ClassInfo), 'Tag', level);
end;

Problem

Using the Typinfo unit, it is easy to enumerate properties as seen in the following snippet: ``` procedure TYRPropertiesMap.InitFrom(AClass: TClass; InheritLevel: Integer = 0); var propInfo: PPropInfo; propCount: Integer; propList: PPropList; propType: PPTypeInfo; pm: TYRPropertyMap; classInfo: TClassInfo; ix: Integer; begin ClearMap; propCount := GetPropList(PTypeInfo(AClass.ClassInfo), propList); for ix := 0 to propCount - 1 do begin propInfo := propList^[ix]; propType := propInfo^.PropType; if propType^.Kind = tkMethod then Continue; // Skip methods { Need to get GetPropInheritenceIndex to work if GetPropInheritenceIndex(propInfo) > InheritLevel then Continue; // Dont include properties deeper than InheritLevel } pm := TYRPropertyMap.Create(propInfo.Name); FList.Add(pm); end; end; ``` However, what I need is to figure out the exact class from which each property inherits. For example in TControl, the Tag property comes from TComponent, which gives it an inheritance depth of 1 (0 being a property declared in TControl itself, such as Cursor). Calculating the inheritance depth is easy if I know which class first defined the property. For my purposes, wherever a property first gained published visibility is where it first appeared. I am using Delphi 2007. Please let me know if more detail is required. All help will be appreciated.

Original source