PDA

Ver la Versión Completa : Tipo de Propiedades y valor de una clase u objeto TypInfo


elarys
27-07-2012, 16:07:22
En realidad no se si estoy haciendo correctamente la pregunta

Aqui mi unidad, solo un formulario (Form1) y un boton (btnPrueba)

unit Unit1;

interface

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

type
TProducto = Class(TPersistent)
private
FCodigo:integer;
FNuevos:boolean;
FNombre:string;
FPrecio:double;
FCreado:TDateTime;
FClaves:TStringList;
public

published
property Codigo: integer read FCodigo write FCodigo;
property Nuevos: boolean read FNuevos write FNuevos;
property Nombre: string read FNombre write FNombre;
property Precio: double read FPrecio write FPrecio;
property Creado: TDateTime read FCreado write FCreado;
property Claves: TStringList read FClaves write FClaves;

Constructor Create;
end;

TForm1 = class(TForm)
btnPrueba: TButton;
procedure btnPruebaClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Producto:TProducto;
procedure GetPropertyList(Obj: TObject; List: TStrings; Filter: TTypeKinds);
public

end;

var
Form1: TForm1;

implementation

{$R *.dfm}

Constructor TProducto.Create;
begin
FClaves := TStringList.Create;
end;

procedure TForm1.btnPruebaClick(Sender: TObject);
var
Value, Tipo: string;
Lista: TStringList;
i: Integer;
XMLFile: TextFile;
PInfo: PPropInfo;
begin
Lista := TStringList.Create;

GetPropertyList(Producto, Lista, tkProperties);

AssignFile(XMLFile, ExtractFilePath(Application.ExeName) + 'prueba.xml');
Rewrite(XMLFile);
WriteLn(XMLFile, '<OBJECT>');

for i := 0 to Lista.Count - 1 do
begin
Value := GetPropValue(Producto, Lista[i]);
PInfo := GetPropInfo(Producto, Lista[i]);
if PInfo <> nil then
begin
case PInfo^.PropType^.Kind of
tkUnknown: Tipo := 'Unknown';
tkInteger: Tipo := 'Integer';
tkChar: Tipo := 'Char';
tkEnumeration: Tipo := 'Enumeration';
tkFloat: Tipo := 'Float';
tkString: Tipo := 'String';
tkSet: Tipo := 'Set';
tkClass: Tipo := 'Class';
tkMethod: Tipo := 'Method';
tkWChar: Tipo := 'WChar';
tkLString: Tipo := 'LString';
tkWString: Tipo := 'WString';
tkVariant: Tipo := 'Variant';
tkArray: Tipo := 'Array';
tkRecord: Tipo := 'Record';
tkInterface: Tipo := 'Interface';
tkInt64: Tipo := 'Int64';
tkDynArray: Tipo := 'DynArray';
end;
end;

WriteLn(XMLFile, ' <' + UpperCase(Lista[i]) + '>' + Value + '</' + UpperCase(Lista[i]) + '>');
end;
WriteLn(XMLFile, '</OBJECT>');
CloseFile(XMLFile);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Producto := TProducto.Create;
Producto.Codigo := 100;
Producto.Nuevos := True;
Producto.Nombre := 'Notebook HP';
Producto.Precio := 3599.99;
Producto.Creado := Date;
Producto.Claves.Add('Codigo');
Producto.Claves.Add('Lugar');
end;

procedure TForm1.GetPropertyList(Obj: TObject; List: TStrings; Filter: TTypeKinds);
var
PropList: PPropList;
Count,i: integer;
begin
List.Clear;
Count := GetPropList(Obj.ClassInfo, Filter, nil);
GetMem(PropList, Count * SizeOf(PPropInfo));
GetPropList(Obj.ClassInfo, Filter, PropList);

for i := 0 to Count -1 do
List.Add(Proplist[i].Name);
end;

end.


Apartir de la clase Producto = Class(TPersistent)
Donde estan definidos sus tipos, quiero por medio de TypInfo o RTTI
Obtener las propiedades y sus respectivos valores.
Vale aclarar que la clase Producto es mucho mas extensa y esta definida en otra unidad, aqui esta resumida para el caso practico.

Con el procedimiento GetPropertyList(Producto, Lista, tkProperties);
Obtengo la lista de propiedades de Producto y se carga en Lista: TStringList
Luego recorriendo ese TStringList obtengo cada uno de los valores y su tipo
Value := GetPropValue(Producto, Lista[i]);
PInfo := GetPropInfo(Producto, Lista[i]);

Mi problema es que al ver los valores siguientes
FCreado:TDateTime;
FClaves:TStringList;
Estos me devuelven numeros de tipo float
Supongamos que en el caso del TDateTime esto sea correcto porque se maneja asi...
Como hago para que eso que me devuelve lo pase a fecha (dd/mm/yyyy) que el usuario ve, siendo que el tipo devuelto es un tkfloat, el usuario no entiende ese numero float, yo tengo que mostrarle la fecha.

En el caso de claves es mas complicado porque lo que me devuelve tambien es un numero float, pero el tipo es un tkClass, como hago para sacar los valores que tiene ese stringlist

Lo que pensaba era, volver a la funcion, donde le paso la clase producto apuntando a claves que es lo que quiero que me devuelva ahora... pero la lista la deja vacia, como que no encuentra ese elemento
GetPropertyList(Producto.Claves, Lista2, tkProperties);

Tambien probe con este procedimiento, tambien me devuelve la lista vacia

GetPropertyNames(Producto.Claves, Lista);

procedure TForm1.GetPropertyNames(Obj: TObject; var PropertyNames: TStringList);
var
TypeInfo: PTypeInfo;
TypeData: PTypeData;
PropList: PPropList;
i: Integer;
begin
if Assigned(PropertyNames) then
begin
PropertyNames.Clear;
TypeInfo := Obj.ClassInfo;
if TypeInfo^.Kind = tkClass then
begin
TypeData := GetTypeData(TypeInfo);

if TypeData^.PropCount > 0 then
begin
PropertyNames.Add(TypeInfo^.Name+':');
new(PropList);
GetPropInfos(TypeInfo, PropList);
for i:=0 to Pred(TypeData^.PropCount) do
if PropertyNames.IndexOf(PropList^[i]^.Name) < 0 then
PropertyNames.Add(PropList^[i]^.Name);
Dispose(PropList)
end;
end;
end;
end;

CSIE
30-07-2012, 10:31:06
Hola,


Para el caso de la propiedad TDateTime basta con usar formatdatetime, para la propiedad TStringList, el valor que te da es un puntero a memoria y puedes hacer un cast, puedes usar la propiedad classname para asegurarte, si haces el cast con una clase distinta, es fácil que obtengas mensajes de Access violation.

Saludos.

Neftali [Germán.Estévez]
30-07-2012, 11:50:53
Yo revisaría los procedimientos y funciones de la unit TypInfo especiales para Clases.
Por ejemplo, puedes hacer algo como esto, sabiendo que la propiedad claves es de tipo "Class":


_obj := GetObjectProp(Producto, 'Claves');


Con eso obtendrás un puntero al objeto el objeto Claves y si preguntan por su ClassName, verás que te devuelve "TStringList".

También hay otra llamada, GetObjectPropClass que te puede ser útil.

elarys
30-07-2012, 20:59:08
Gracias Neftali, justamente estaba viendo las funciones que propones, les dejo un pequeño resumen de como lo estoy resolviendo, por si a alguno le interesa. Por ahora para ir tomando la idea estoy guardando los datos obtenidos en un array dinamico. Y con el procedimiento recursivo.


//La llamada a la funcion seria algo como
GetPropertyList(Producto, tkProperties, 0);

//Y aca dicha funcion
procedure GetPropertyList(Obj: TObject; Filter: TTypeKinds; Counter: integer);
var
Names, Value, Tipo: string;
PInfo: PPropInfo;
PropList: PPropList;
Count, i, j: integer;
Ob: TObject;
begin
//Aqui obtengo la lista de propiedades del objeto
Count := GetPropList(Obj.ClassInfo, Filter, nil);
GetMem(PropList, Count * SizeOf(PPropInfo));
GetPropList(Obj.ClassInfo, Filter, PropList);
//---

//Luego las recorro y obtengo
for i := 0 to Count -1 do
begin
Value := GetPropValue(Obj, Proplist[i].Name); //Valor de cada propiedad
PInfo := GetPropInfo(Obj, Proplist[i].Name); //info para obtener tipo de propiedad luego
Names := UpperCase(Proplist[i].Name); //Nombre de cada propiedad

//Aqui segun la informacion de la propiedad obtengo el tipo
//Añadi todos los casos posibles
//En el caso de que la propiedad sea TDateTime claro con un formatdatetime se soluciona
//pero el tipo que me devuelve cuando es tdatetime es float... entonces cuando me lee FPrecio
//lo transformaria tambien con formatdatetime siendo que este si es float y no hay que modificarlo.
//Estoy viendo que puedo hacer al respecto

if PInfo <> nil then
begin
case PInfo^.PropType^.Kind of
tkUnknown: Tipo := 'Unknown';
tkInteger: Tipo := 'Integer';
tkChar: Tipo := 'Char';
tkEnumeration: Tipo := 'Enumeration';
tkFloat: Tipo := 'Float';
tkString: Tipo := 'String';
tkSet: Tipo := 'Set';
tkClass: Tipo := 'Class';
tkMethod: Tipo := 'Method';
tkWChar: Tipo := 'WChar';
tkLString: Tipo := 'LString';
tkWString: Tipo := 'WString';
tkVariant: Tipo := 'Variant';
tkArray: Tipo := 'Array';
tkRecord: Tipo := 'Record';
tkInterface: Tipo := 'Interface';
tkInt64: Tipo := 'Int64';
tkDynArray: Tipo := 'DynArray';
end;
end;

//Si es de tipo clase, llamo nuevamente mi procedimiento de forma recursiva
if Tipo = 'Class' then
begin
j := High(xmlArray) + 1;
SetLength(xmlArray, j + 1);
SetLength(xmlArray[j], 1);
xmlArray[j][0] := '<' + Names + '>';

Ob := GetObjectProp(Obj, Proplist[i].Name);
GetPropertyList(Ob, tkProperties, Counter + 1);

j := High(xmlArray) + 1;
SetLength(xmlArray, j + 1);
SetLength(xmlArray[j], 1);
xmlArray[j][0] := + '< /' + Names + '>';
//Esta parte '< /' debe ir sin espacios pero las etiquetas delphi de la pagina me las borra
//si le quito el espacio, si no se quita el espacio al querer leer el xml da error
end
else
begin
//Por ahora si es de otro tipo solo lo cargo en un array
//como si lo tuviera que escribir en xml
j := High(xmlArray) + 1;
SetLength(xmlArray, j + 1);
SetLength(xmlArray[j], 1);
xmlArray[j][0] := '<' + Names + '>' + Value + '< /' + Names + '>';
end;
end;
end;

elarys
30-07-2012, 21:34:03
{
En el procedimiento anterior el resultado lo pasaba a un array dinamico porque pensaba
que me hacian falta mas lineas, en este el resultado se carga en un stringlist y me ahorro
como 5 lineas de codigo jejeje, ver List.Add
}

procedure GetPropertyList(Obj: TObject; Filter: TTypeKinds; List: TStringList; Counter: integer);
var
Names, Value, Tipo: string;
PInfo: PPropInfo;
PropList: PPropList;
Count, i, j: integer;
Ob: TObject;
begin
Count := GetPropList(Obj.ClassInfo, Filter, nil);
GetMem(PropList, Count * SizeOf(PPropInfo));
GetPropList(Obj.ClassInfo, Filter, PropList);

for i := 0 to Count -1 do
begin
Value := GetPropValue(Obj, Proplist[i].Name);
PInfo := GetPropInfo(Obj, Proplist[i].Name);
Names := UpperCase(Proplist[i].Name);

if PInfo <> nil then
begin
case PInfo^.PropType^.Kind of
tkUnknown: Tipo := 'Unknown';
tkInteger: Tipo := 'Integer';
tkChar: Tipo := 'Char';
tkEnumeration: Tipo := 'Enumeration';
tkFloat: Tipo := 'Float';
tkString: Tipo := 'String';
tkSet: Tipo := 'Set';
tkClass: Tipo := 'Class';
tkMethod: Tipo := 'Method';
tkWChar: Tipo := 'WChar';
tkLString: Tipo := 'LString';
tkWString: Tipo := 'WString';
tkVariant: Tipo := 'Variant';
tkArray: Tipo := 'Array';
tkRecord: Tipo := 'Record';
tkInterface: Tipo := 'Interface';
tkInt64: Tipo := 'Int64';
tkDynArray: Tipo := 'DynArray';
end;
end;

if Tipo = 'Class' then
begin
List.Add('<' + Names + '>');
Ob := GetObjectProp(Obj, Proplist[i].Name);
GetPropertyList(Ob, tkProperties, List, Counter + 1);
List.Add('< /' + Names + '>');
end
else
begin
List.Add('<' + Names + '>' + Value + '< /' + Names + '>');
end;
end;
end;

Neftali [Germán.Estévez]
31-07-2012, 10:22:49
5 lineas, son 5 líneas... ;)