Typinfo ユニットを使用すると、次のスニペットに示すように、プロパティを簡単に列挙できます。
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;
ただし、必要なのは、各プロパティが継承する正確なクラスを把握することです。たとえば、TControl では、Tag プロパティは TComponent から取得され、継承の深さは 1 になります (0 は、TControl 自体で宣言された Cursor などのプロパティです)。
どのクラスが最初にプロパティを定義したかがわかれば、継承の深さを計算するのは簡単です。私の目的では、プロパティが最初に公開された場所は、最初に表示された場所です。
Delphi 2007 を使用しています。詳細が必要な場合はお知らせください。すべての助けに感謝します。