Delphi类的入门例子(9): 获取对象的 RTTI 属性与事件的函数

unit Unit1;

interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, xmldom, XMLIntf, XMLBrokr, msxmldom, XMLDoc;
type
 TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  Memo1: TMemo;
  Memo2: TMemo;
  XMLDocument1: TXMLDocument;
  procedure Button1Click(Sender: TObject);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
uses TypInfo; {获取类的信息, 需要这个单元}

//获取对象的 RTTI 属性与事件的函数

function GetPropertyAndEventList(obj: TObject; pList,eList: TStringList): Boolean;
var
 ClassTypeInfo: PTypeInfo; {类的信息结构指针}
 ClassDataInfo: PTypeData; {类的数据结构指针}
 propertyList : PPropList; {TPropInfo 是属性的数据结构;
               PPropList 是其指针;
               TPropList 是属性结构指针的列表数组;
               PPropList 是指向这个数组的指针}
 num : Integer;      {记录属性的总数}
 size: Integer;      {记录属性结构的大小}
 i: Integer;
begin
 ClassTypeInfo := obj.ClassInfo;       {先获取: 类的信息结构指针}
 ClassDataInfo := GetTypeData(ClassTypeInfo); {再获取: 类的数据结构指针}
 num := ClassDataInfo.PropCount;       {属性总数}
 size := SizeOf(TPropInfo);          {属性结构大小}
 GetMem(propertyList, size*num);       {给属性数组分配内存}
 GetPropInfos(ClassTypeInfo, propertyList);  {获取属性列表}
 for i := 0 to num - 1 do
 begin
  if propertyList[i].PropType^.Kind = tkMethod then {如果是事件; 事件也是属性吗, 给分出来}
   eList.Add(propertyList[i].Name)
  else
   pList.Add(propertyList[i].Name);
 end;
 pList.Sort; eList.Sort; {排序}
 FreeMem(propertyList); {释放属性数组的内存}
 Result := True;
end;

//测试

procedure TForm1.Button1Click(Sender: TObject);
var
 PL,EL: TStringList;
begin
 PL := TStringList.Create;
 EL := TStringList.Create;
 Memo1.Clear;
 Memo2.Clear;
 GetPropertyAndEventList(Self, PL, EL); {调用函数, 第一个参数是对象名}
 Memo1.Lines := PL;
 Memo2.Lines := EL;
 PL.Free;
 EL.Free;
end;

end.

时间: 2024-09-17 03:36:56

Delphi类的入门例子(9): 获取对象的 RTTI 属性与事件的函数的相关文章

Delphi类的入门例子(7): 遍历窗体的所有父类

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender:

Delphi类的入门例子(4): property

//类单元unit Person; interface type TPerson = class(TObject) private FName: string; FAge: Integer; public procedure SetName(const strName: string); procedure SetAge(const intAge: Integer); property Name: string read FName write SetName; property Age: In

Delphi类的入门例子(3): Create与Destroy

//类单元unit Person; interface uses Dialogs; type TPerson = class(TObject) private FName: string; FAge: Integer; public constructor Create(strName: string; intAge: Integer); destructor Destroy; override; function GetName: string; function GetAge: Intege

Delphi类的入门例子(2): 数字盒子

//类单元 unit NumBox; interface type TNumBox = class private FCount: Integer; public procedure AddOne; procedure AddFive; procedure ZeroCount; function GetCount: Integer; end; implementation { TNumBox } procedure TNumBox.AddOne; begin Inc(FCount); end;

Delphi类的入门例子(8): 遍历窗体中所有控件的函数

//显示窗体中所有控件的函数 function GetCtrls(Control: TWinControl; List: TStringList): Boolean; var i: Integer; obj: TWinControl; begin for i := 0 to Control.ControlCount-1 do begin obj := TWinControl(Control.Controls[i]); List.Add(obj.Name); //如果控件中包含其他控件, 则递归调

关于ajax对象一些常用属性、事件和方法大小写比较常见的问题总结

最近比较空闲,于是抽个时间整理些关于ajax方法的东东.在项目中经常发现ajax板块好多问题都是属性,方法,事件大小写不区分问题,最终导致了程序运行出现麻烦. 下面是ajax对象的一些常用属性,事件和方法 1)标准的ajax对象的属性有readyState,status,responseText,responseXML 2)非标准ajax对象属性,针对IE浏览器的,有responseBody,2进制数据流.如果不考虑浏览器兼容,这个属性+VBScript能很好的解决乱码问题. Visual Ba

Delphi类的入门的例子(6): 类引用示例

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) RadioGroup1: TRadioGroup; procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shi

Delphi类的入门的例子(5): override

unit Unit1; interface uses Classes, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; TBase = class procedure proc1; virtual; procedure proc2; end; TChild = class(TBase) pro

JavaScript对象的property属性详解

 这篇文章主要介绍了JavaScript对象的property属性,详细讲解了property的各种属性,需要的朋友可以参考下 JavaScript中对象的property有三个属性: 1.writable.该property是否可写. 2.enumerable.当使用for/in语句时,该property是否会被枚举. 3.configurable.该property的属性是否可以修改,property是否可以删除.   在ECMAScript 3标准中,上面三个属性的值均为true且不可改: