每日程炼-Delphi 2010 新增功能之: Rtti 单元读取或者设置类属性

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

通过 Rtti 还能够调用一个类的方法, 也能读取或设置其属性值.

Demo下载Rtti单元读取设置一个类

 

代码部分: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) btn1: TButton; btn2: TButton; btn3: TButton; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); procedure btn3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; {自定义的类} TMyClass = class(TComponent) public procedure msg(const str: string); function Add(const a,b: Integer): Integer; end; var Form1: TForm1; implementation uses Rtti; {$R *.dfm} { MyClass 类的实现 -----------------------------------------------------------} procedure TMyClass.msg(const str: string); begin MessageDlg(str, mtInformation, [mbYes], 0); end; function TMyClass.Add(const a, b: Integer): Integer; begin Result := a + b; end; procedure TForm1.btn1Click(Sender: TObject); var obj: TMyClass; t: TRttiType; m1,m2: TRttiMethod; r: TValue; //TRttiMethod.Invoke 的返回类型 begin t := TRttiContext.Create.GetType(TMyClass); {获取 TMyClass 类的两个方法} m1 := t.GetMethod('msg'); {procedure} m2 := t.GetMethod('Add'); {function} obj := TMyClass.Create(Self); {调用需要依赖一个已存在的对象} {调用 msg 过程} m1.Invoke(obj, ['Delphi 2010']); {将弹出信息框} {调用 Add 函数} r := m2.Invoke(obj, [1, 2]); {其返回值是个 TValue 类型的结构} ShowMessage(IntToStr(r.AsInteger)); {3} obj.Free; end; procedure TForm1.btn2Click(Sender: TObject); var obj: TMyClass; t: TRttiType; p: TRttiProperty; r: TValue; begin obj := TMyClass.Create(Self); t := TRttiContext.Create.GetType(TMyClass); p := t.GetProperty('Name'); p.SetValue(obj, 'NewName'); r := p.GetValue(obj); ShowMessage(r.AsString); {NewName} obj.Free; end; procedure TForm1.btn3Click(Sender: TObject); var t: TRttiType; p: TRttiProperty; r: TValue; begin t := TRttiContext.Create.GetType(TButton); p := t.GetProperty('Align'); p.SetValue(btn3, TValue.FromOrdinal(TypeInfo(TAlign), Ord(alLeft))); r := p.GetValue(btn3); ShowMessage(IntToStr(r.AsOrdinal)); {3} end; end.   窗体部分: object Form1: TForm1 Left = 0 Top = 0 Caption = #24858#20154#31508#35760' - http://www.yrnote.com' ClientHeight = 144 ClientWidth = 578 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object btn1: TButton Left = 72 Top = 66 Width = 105 Height = 57 Caption = #35843#29992#23384#22312#30340#23545#35937#31867 TabOrder = 0 OnClick = btn1Click end object btn2: TButton Left = 208 Top = 69 Width = 154 Height = 52 Caption = #20462#25913#19968#20010#31867#26576#20010#23454#20363#30340#23646#24615 TabOrder = 1 OnClick = btn2Click end object btn3: TButton Left = 384 Top = 69 Width = 161 Height = 52 Caption = #20462#25913'button'#31867#35813#25353#38062#30340#23646#24615 TabOrder = 2 OnClick = btn3Click end end

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

发表评论

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