http://blog.csdn.net/easyboot/article/details/8004954

Delphi 遍历类中的属性

2012-09-21 16:45 2125人阅读 评论(1) 收藏 举报
 分类:
Delphi(54) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls,TypInfo;
  6. type
  7. TForm1 = class(TForm)
  8. Button2: TButton;
  9. procedure Button1Click(Sender: TObject);
  10. procedure Button2Click(Sender: TObject);
  11. private
  12. { Private declarations }
  13. public
  14. { Public declarations }
  15. end;
  16. {$M+} //如果不用 {$M+} 则需要继承TPersistent
  17. TTest = class(TObject)
  18. public
  19. FName,FSex,FScholl:string;
  20. published
  21. property Name :string read FName write FName;
  22. property Sex :string read FSex write FSex;
  23. property Scholl :string read FScholl write FScholl;
  24. end;
  25. {$M-}
  26. var
  27. Form1: TForm1;
  28. implementation
  29. {$R *.dfm}
  30. procedure TForm1.Button2Click(Sender: TObject);
  31. var
  32. PropCount, I: SmallInt;
  33. PropList: PPropList;
  34. PropStr,sValues: string;
  35. AClass:TTest;
  36. AStrings:TStrings;
  37. begin
  38. AStrings:=TStrings.Create;
  39. AClass:=TTest.Create;
  40. AClass.Name:='MyTest';
  41. AClass.Sex:='Male';
  42. AClass.Scholl:='Scholl';
  43. PropCount := GetTypeData(AClass.ClassInfo).PropCount;
  44. GetPropList(AClass.ClassInfo, PropList);
  45. for I := 0 to PropCount - 1 do
  46. begin
  47. case PropList[I]^.PropType^.Kind of
  48. tkClass      : PropStr := '[Class] ';
  49. tkMethod     : PropStr := '[Method]';
  50. tkSet        : PropStr := '[Set]   ';
  51. tkEnumeration: PropStr := '[Enum]  ';
  52. else
  53. PropStr := '[Field] ';
  54. end;
  55. PropStr := PropStr + PropList[I]^.Name;
  56. PropStr := PropStr + ': ' + PropList[I]^.PropType^.Name;
  57. sValues:=GetPropValue(AClass,PropList[I].Name,True);
  58. ShowMessage(sValues);
  59. ShowMessage(PropStr);
  60. end;
  61. FreeMem(PropList);
  62. end;
  63. end.

Delphi 遍历类中的属性的更多相关文章

  1. Java 遍历类中的属性

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  2. java中遍历类中的属性、调用getter&setter方法

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  3. 转载:java 中对类中的属性使用set/get方法的意义和用法

    经常看到有朋友提到类似:对类中的属性使用set/get方法的作用?理论的回答当然是封闭性之类的,但是这样对我们有什么作用呢?为什么要这样设计?我直接使用属性名来访问不是更直接,代码更简洁明了吗?下面我 ...

  4. 尚硅谷面试第一季-11MyBatis中当实体类中的属性名和表中的字段名不一样怎么办

    问题: MyBatis中当实体类中的属性名和表中的字段名不一样 ,怎么办 ? 解决方案: 1.写sql语句时起别名 <!-- id属性:必须是接口中方法的方法名 resultType属性:必须是 ...

  5. 关于System类中out属性 实例化的问题

    System类中out属性的声明是这样的: public final static PrintStream out = nullPrintStream(); private static PrintS ...

  6. 使用反射获取类中的属性(可用于动态返回PO类的列,当做表格的表头)

    //利用反射取类中的属性字段 try { Class clazz = Class.forName("houji.bean.model.TaskModel"); Field[] fi ...

  7. Python 简明教程 --- 20,Python 类中的属性与方法

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 与客户保持良好的关系可以使生产率加倍. -- Larry Bernstain 目录 类中的变量称为属 ...

  8. Java初学者作业——定义英雄类(Hero),英雄类中的属性包括:姓名、攻击力、防御力、生命值和魔法值;方法包括:攻击、介绍。

    返回本章节 返回作业目录 需求说明: 定义英雄类(Hero),英雄类中的属性包括:姓名.攻击力.防御力.生命值和魔法值:方法包括:攻击.介绍. 实现思路: 分析类的属性及其变量类型. 分析类的方法及其 ...

  9. Java初学者作业——定义管理员类(Admin),管理员类中的属性包括:姓名、账号、密码、电话;方法包括:登录、显示自己的信息。

    返回本章节 返回作业目录 需求说明: 定义管理员类(Admin),管理员类中的属性包括:姓名.账号.密码.电话:方法包括:登录.显示自己的信息. 实现思路: 分析类的属性及其变量类型. 分析类的方法及 ...

随机推荐

  1. 出去就餐并且理解Express.js的基本知识

    Going out to eat and understanding the basics of Express.js出去就餐并且理解Express.js的基本知识 原文:Going out to e ...

  2. JAVA中自定义properties文件介绍

    Gradle中的使用 1. 使用gradle.properties buid.gradle 和 gradle.properties可以项目使用,在同一个项目中,build.gradle可以直接获取其同 ...

  3. 028-实现阿里云ESC多FLAT网络

    实现类似于阿里云ECS的网络结构,其效果为云主机拥有两块和两个不同的网络,一个网络是用于用于和外网连接,一个用于内网通信,但宿主机上至少有两个网卡,整体配置如下:1.在wmare里给宿主机添加一块网卡 ...

  4. 树莓派上固定ip

    sudo nano /etc/dhcpcd.conf interface eth0 static ip_address=192.168.123.99/24 static routers=192.168 ...

  5. vim比较文件

    横向分割显示: $ vim -o filename1 filename2 纵向分割显示: $ vim -O filename1 filename2 ctl w w 切换文件

  6. qt配置opengl

    cmake 编译opengl,参考https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows cmake configure完成没有错误后,点击g ...

  7. [AI]cognitive business|cognitive thinking

    just watch the cognitive business presentation of IBM CEO Ginni. here is my thoughts in this field : ...

  8. linux find 用法和常见用例

    Linux中find常见用法示例 ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \; 本人qq群也有许多 ...

  9. gperftools尝试

    最近在找windows下比较好用的函数时间统计的库,听同事说gperftools是跨平台的,就下载下来尝试了一把.发现它确实实现了windows上可以调用的dll库文件(tcmalloc_minima ...

  10. bzoj5090 [Lydsy1711月赛]组题 分数规划

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5090 题解 令 \(s_i\) 表示 \(a_i\) 的前缀和. 那么平均难度系数就是 \[ ...