quarta-feira, 22 de outubro de 2008

Listar as propriedades de um objeto

Tenho um objeto, gostaria de listar todas as propriedades deste objeto e seus tipos sem necessariamente saber quais são.
Fonte

unit UnitX;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, TypInfo,
Dialogs, StdCtrls;

type
TMinhaClasse = class(TObject)
private
FRetornou: Boolean;
FCodigo: Integer;
FNome: string;
published
property Nome: string read FNome write FNome;
property Codigo: Integer read FCodigo write FCodigo;
property Retornou: Boolean read FRetornou write FRetornou;
end;

TForm9 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
end;

var
Form9: TForm9;

implementation

{$R *.dfm}

procedure TForm9.Button1Click(Sender: TObject);
var
Count, I: Integer;
List : TPropList;
MinhaClasse : TMinhaClasse;
Info : PPropInfo;
begin
MinhaClasse := TMinhaClasse.Create;
Count := GetPropList(TypeInfo(TMinhaClasse), tkProperties, @List);

for I := 0 to Pred(Count) do
begin
Info := GetPropInfo(TypeInfo(TMinhaClasse), List[I]^.Name);
Listbox1.Items.Add(List[I]^.Name + ' : ' + Info^.PropType^.Name);
end;

MinhaClasse.Free;
end;

end.

Saída na ListBox em tela
Nome : string;
Retornou : Boolean;
Codigo : Integer;

  © Blogger template 'Perfection' by Ourblogtemplates.com 2008

Back to TOP