使用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.如果需要更多详细信息,请告诉我.所有帮助将不胜感激.
解决方法
这对我有用.
关键是从传递的子TypeInfo中获取父类的TypeInfo
关键是从传递的子TypeInfo中获取父类的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;
原文地址:https://www.jb51.cc/delphi/101810.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。