Here is a simple example of how to go through a property of type TObjectList<T>
, no doubt there are other numerous ways to accomplish this task, as the goal here is to show the way and simplicity this is the best option. This example is not validating properties or without access methods getters&setters
is writing and reading everything directly from the instance variables campos/atributos da classe
unit FrmPrincipal;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
System.Generics.Collections, System.Rtti;
type
TCampoValidacao = class
private
FId: string;
FNome: string;
FEndereco: string;
FBairro: string;
public
property Id: string read FId write FId;
property Nome: string read FNome write FNome;
property Endereco: string read FEndereco write FEndereco;
property Bairro: string read FBairro write FBairro;
end;
TBaseModelo = class(TInterfacedObject)
private
FListaCamposValidacao: TObjectList<TCampoValidacao>;
public
property ListaCamposValidacao: TObjectList<TCampoValidacao> read FListaCamposValidacao write FListaCamposValidacao;
constructor Create;
destructor Destroy; override;
end;
TModeloX = Class(TBaseModelo )
private
FId:string;
FNome: string;
FEndereco: string;
FBairro: string;
public
property Id: string read FId write FId;
property Nome: string read FNome write FNome;
property Endereco: string read FEndereco write FEndereco;
property Bairro: string read FBairro write FBairro;
End;
TForm1 = class(TForm)
btGerar: TButton;
Memo1: TMemo;
procedure btGerarClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btGerarClick(Sender: TObject);
var
id: TGuid;
meuModelo: TModeloX;
campoValidacao: TCampoValidacao;
ctxRtti : TRttiContext;
typeRtti : TRttiType;
propRtti : TRttiProperty;
I: Integer;
begin
meuModelo := TModeloX.Create;
for I := 0 to 50 do
begin
CreateGUID(id);
campoValidacao := TCampoValidacao.Create;
campoValidacao.Id := System.SysUtils.GUIDToString( id );
campoValidacao.Nome := 'MAURO' + IntToStr( i );
campoValidacao.Endereco := 'RUA PEDRO PEREIRA - Nº ' + IntToStr( i );
campoValidacao.Bairro := 'Vila Martins ';
meuModelo.ListaCamposValidacao.Add(campoValidacao);
end;
campoValidacao := nil;
ctxRtti := TRttiContext.Create;
for campoValidacao in meuModelo.ListaCamposValidacao do
begin
typeRtti := ctxRtti.GetType( campoValidacao.ClassType );
for propRtti in typeRtti.GetProperties do
Memo1.Lines.Add( propRtti.Name+': ' + propRtti.GetValue( campoValidacao).ToString );
end;
ctxRtti.Free;
end;
{ TBaseModelo }
constructor TBaseModelo.Create;
begin
FListaCamposValidacao := TObjectList<TCampoValidacao>.Create;
end;
destructor TBaseModelo.Destroy;
begin
if Assigned( FListaCamposValidacao ) then
FListaCamposValidacao.Free;
inherited;
end;
end.
Research source: http://edn.embarcadero.com/article/41728
Can you get more information on how you want to access the Press? Already know the name of the Property beforehand or you will have to find the name of the Property by RTTI tb?
– Caputo