Podemos listar todas las propiedades published de un componente utilizando RTTI. Para ver el funcionamiento podemos crear un formulario con un componente TMemo y un Botón.
El procedimieno que utilizamos para obtener las propiedades es el siguiente:
Código Delphi
[-]
procedure ListComponentProperties(Componente:TComponent; Strings: TStrings);
var
Count, Size, j: Integer;
lista: PPropList;
PropInfo: PPropInfo;
PropOrEvent, PropValue: String;
begin
Count := GetPropList(Componente.ClassInfo, tkAny, nil);
Size := Count * SizeOf(Pointer);
GetMem(lista, Size);
try
Count := GetPropList(Componente.ClassInfo, tkAny, lista);
for j := 0 to Count - 1 do begin
PropInfo := lista^[j];
if (PropInfo^.PropType^.Kind in tkMethods) then begin
PropOrEvent := 'Event'
end
else begin
PropOrEvent := 'Property';
end;
PropValue := VarToStr(GetPropValue(Componente, PropInfo^.Name));
Strings.Add(Format('[%s] Nombre:%s Tipo:%s Valor:%s',
[PropOrEvent, PropInfo^.Name, PropInfo^.PropType^.Name, PropValue]));
end;
finally
FreeMem(lista);
end;
end;
Para utilizarlo podemos usar el siguiente código:
Código Delphi
[-]
Memo1.Lines.BeginUpdate;
Memo1.Lines.Clear;
ListComponentProperties(Button1, Memo1.Lines);
Memo1.Lines.EndUpdate;
Se debe añadir la Unit
TypInfo al USES.