使用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)。
如果我知道哪个类首先定义了属性,则计算继承深度很容易。就我的目的而言,无论财产第一次获得发布可见性的地方都是它首次出现的地方。
我正在使用德尔福2007年。请让我知道是否需要更多的细节。所有的帮助将不胜感激。
啊......我明白了。希望有一个更直接的方式(例如与VMT搞混),但这绝对有效。幸运的是效率目前并不是我主要关心的问题。感谢您的快速回答 - 您的解决方案绝对在* direct *框之外,我被卡住了。 – Atorian 2009-10-14 11:32:53