Delphi 遍历类中的属性

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

Delphi 遍历类中的属性

标签: delphistringbuttonclassformswindows
 分类:
 
[delphi] view plain copy
 
 print?
  1. unit Unit1;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, StdCtrls,TypInfo;  
  8.   
  9. type  
  10.   TForm1 = class(TForm)  
  11.     Button2: TButton;  
  12.     procedure Button1Click(Sender: TObject);  
  13.     procedure Button2Click(Sender: TObject);  
  14.   private  
  15.     { Private declarations }  
  16.   public  
  17.     { Public declarations }  
  18.   end;  
  19. {$M+} //如果不用 {$M+} 则需要继承TPersistent  
  20.   TTest = class(TObject)  
  21.   public  
  22.   
  23.   
  24.    FName,FSex,FScholl:string;  
  25.   published  
  26.      property Name :string read FName write FName;  
  27.      property Sex :string read FSex write FSex;  
  28.      property Scholl :string read FScholl write FScholl;  
  29.     end;  
  30. {$M-}  
  31. var  
  32.   Form1: TForm1;  
  33.   
  34. implementation  
  35.   
  36. {$R *.dfm}  
  37.   
  38.   
  39. procedure TForm1.Button2Click(Sender: TObject);  
  40. var  
  41.  PropCount, I: SmallInt;  
  42.  PropList: PPropList;  
  43.  PropStr,sValues: string;  
  44.  AClass:TTest;  
  45.  AStrings:TStrings;  
  46. begin  
  47.   AStrings:=TStrings.Create;  
  48.   AClass:=TTest.Create;  
  49.   AClass.Name:='MyTest';  
  50.   AClass.Sex:='Male';  
  51.   AClass.Scholl:='Scholl';  
  52.   
  53.  PropCount := GetTypeData(AClass.ClassInfo).PropCount;  
  54.  GetPropList(AClass.ClassInfo, PropList);  
  55.  for I := to PropCount - do  
  56.  begin  
  57.    case PropList[I]^.PropType^.Kind of  
  58.      tkClass      : PropStr := '[Class] ';  
  59.      tkMethod     : PropStr := '[Method]';  
  60.      tkSet        : PropStr := '[Set]   ';  
  61.      tkEnumeration: PropStr := '[Enum]  ';  
  62.    else  
  63.      PropStr := '[Field] ';  
  64.    end;  
  65.    
  66.    PropStr := PropStr + PropList[I]^.Name;  
  67.    PropStr := PropStr + ': ' + PropList[I]^.PropType^.Name;     
  68.   
  69.    sValues:=GetPropValue(AClass,PropList[I].Name,True);  
  70.    ShowMessage(sValues);  
  71.    ShowMessage(PropStr);  
  72.  end;  
  73.  FreeMem(PropList);  
  74.   
  75.    
  76. end;  
  77.   
  78. end.  
delphi lazarus opengl 网页操作自动化, 图像分析破解,游戏开发
原文地址:https://www.cnblogs.com/delphi-xe5/p/5382162.html