【发布时间】:2010-12-06 15:23:59
【问题描述】:
使用 Typinfo 单元,很容易枚举属性,如下面的 sn-p 所示:
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。如果需要更多详细信息,请告诉我。我们将不胜感激。
【问题讨论】:
标签: delphi properties enumeration rtti