rtti读取和设置属性
2024-09-03 13:39:29
http://www.cnblogs.com/hnxxcxg/archive/2013/03/02/2940565.html
rtti读取和设置属性
编辑器通过 Rtti 还能够调用一个类的方法, 也能读取或设置其属性值. -------------------------------------------------------------------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; {自定义的类} TMyClass = class(TComponent) public procedure msg(const str: string); function Add(const a,b: Integer): Integer; end; var Form1: TForm1; implementation {$R *.dfm} uses Rtti; { 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; //通过 Rtti 的手段使用 TMyClass 类的方法 -------------------------------------- procedure TForm1.Button1Click(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; //通过 Rtti 的手段修改并获取 TMyClass 类的属性 -------------------------------- procedure TForm1.Button2Click(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; end. -------------------------------------------------------------------------------- 回复下面的问题:如何设置枚举值: -------------------------------------------------------------------------------- procedure TForm1.Button3Click(Sender: TObject); var t: TRttiType; p: TRttiProperty; r: TValue; begin t := TRttiContext.Create.GetType(TButton); p := t.GetProperty('Align'); p.SetValue(Button1, TValue.FromOrdinal(TypeInfo(TAlign), Ord(alLeft))); r := p.GetValue(Button1); ShowMessage(IntToStr(r.AsOrdinal)); {3} end; 加载中...
分类: delphi新语法
最新文章
- ThinkPHP3.2.3整合smarty模板(一)
- Pyqt+QRcode 生成 识别 二维码
- js 判断所选时间(或者当前时间)是否在某一时间段用于不同时间段显示不同的客服qq
- duxcms SQL Injection In /admin/module/loginMod.class.php
- 以对象的方式来访问xml数据表(三)
- andriod ==和equals
- openstack4j
- 新浪实时股票数据接口http://hq.sinajs.cn/list=code
- WEB 开发异常:java.lang.ClassNotFoundException
- [AngularJS] Enable Animations Explicitly For A Performance Boost In AngularJS
- Java学习日记-6 继承
- jQuery 源码分析和使用心得 - core.js
- Git学习 -- 标签管理
- C++第四篇--重载_指针_引用
- 错误问题:OpenGL version to old,GLViewinitWithRect(const stdbasic_stringchar,stdchar_traitschar,stdalloca
- STL:deque用法详解
- vue中使用Base64和md5和rsa
- java删除文件支持通配符
- C# 自定义配置文件
- FileNotFoundError: [Errno 2] No such file or directory的解决方法
热门文章
- python3中编码与解码的问题
- MongoDB入门_MongoDB特色
- 北京太速科技股份有限公司产品手册V201903020
- HBase(三)——搭建Fully-distributed
- Granger Causality 格兰杰因果关系
- amazon-aws 使用 SNS 发送短信
- u-boot-2016.09 make编译过程分析(一)
- Java数据封装成树形结构,多级
- thinkphp5.0.19 request
- 关于mysql(Navicat premium软件中) 外键设置中“删除”和“更新”选项详解