每日程炼-delphi2010 Rtti获取某一个类的信息

  • A+
所属分类:[开发技巧]

2010新增的Rtti单元可以轻松的获取某一个指定的类的信息。

Demo下载Rtti 单元(1)

程序代码:

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) mmo1: TMemo; btn1: TButton; btn2: TButton; btn3: TButton; btn4: TButton; btn5: TButton; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); procedure btn3Click(Sender: TObject); procedure btn4Click(Sender: TObject); procedure btn5Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses Rtti; //TRttiContext.GetTypes procedure TForm1.btn1Click(Sender: TObject); var ctx: TRttiContext; t: TRttiType; begin mmo1.Clear; for t in ctx.GetTypes do mmo1.Lines.Add(t.Name); end; //获取 TButton 类的方法 procedure TForm1.btn2Click(Sender: TObject); var ctx: TRttiContext; t: TRttiType; m: TRttiMethod; begin mmo1.Clear; t := ctx.GetType(TButton); for m in t.GetMethods do mmo1.Lines.Add(m.ToString); end; //获取 TButton 类的属性 procedure TForm1.btn3Click(Sender: TObject); var ctx: TRttiContext; t: TRttiType; p: TRttiProperty; begin mmo1.Clear; t := ctx.GetType(TButton); //for p in t.GetProperties do Memo1.Lines.Add(p.Name); for p in t.GetProperties do mmo1.Lines.Add(p.ToString); end; //获取 TButton 类的字段 procedure TForm1.btn4Click(Sender: TObject); var ctx: TRttiContext; t: TRttiType; f: TRttiField; begin mmo1.Clear; t := ctx.GetType(TButton); //for f in t.GetFields do Memo1.Lines.Add(f.Name); for f in t.GetFields do mmo1.Lines.Add(f.ToString); end; //获取全部 procedure TForm1.btn5Click(Sender: TObject); var ctx: TRttiContext; t: TRttiType; ms: TArray<TRttiMethod>; ps: TArray<TRttiProperty>; fs: TArray<TRttiField>; begin mmo1.Clear; t := ctx.GetType(TButton); ms := t.GetMethods; ps := t.GetProperties; fs := t.GetFields; mmo1.Lines.Add(Format('%s 类共有 %d 个方法', [t.Name, Length(ms)])); mmo1.Lines.Add(Format('%s 类共有 %d 个属性', [t.Name, Length(ps)])); mmo1.Lines.Add(Format('%s 类共有 %d 个字段', [t.Name, Length(fs)])); end; end.

 

窗体代码:

object Form1: TForm1 Left = 0 Top = 0 Caption = #24858#20154#31508#35760' htttp:///www.yrnote.com' ClientHeight = 290 ClientWidth = 526 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object mmo1: TMemo Left = 24 Top = 24 Width = 393 Height = 241 ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861 Lines.Strings = ( 'mmo1') TabOrder = 0 end object btn1: TButton Left = 436 Top = 80 Width = 75 Height = 25 Caption = 'GetTypes' TabOrder = 1 OnClick = btn1Click end object btn2: TButton Left = 436 Top = 120 Width = 75 Height = 25 Caption = 'GetMethods' TabOrder = 2 OnClick = btn2Click end object btn3: TButton Left = 436 Top = 160 Width = 75 Height = 25 Caption = 'GetProperties' TabOrder = 3 OnClick = btn3Click end object btn4: TButton Left = 436 Top = 200 Width = 75 Height = 25 Caption = 'GetFields' TabOrder = 4 OnClick = btn4Click end object btn5: TButton Left = 436 Top = 240 Width = 75 Height = 25 Caption = 'GetAll' TabOrder = 5 OnClick = btn5Click end end

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: