Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 27-07-2012
elarys elarys is offline
Miembro
 
Registrado: abr 2007
Posts: 94
Poder: 18
elarys Va por buen camino
Tipo de Propiedades y valor de una clase u objeto TypInfo

En realidad no se si estoy haciendo correctamente la pregunta

Aqui mi unidad, solo un formulario (Form1) y un boton (btnPrueba)
Código Delphi [-]
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, '');

  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, '');
  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);
Código Delphi [-]
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;
Responder Con Cita
  #2  
Antiguo 30-07-2012
CSIE CSIE is offline
Miembro
 
Registrado: feb 2008
Ubicación: Universo paralelo
Posts: 69
Poder: 17
CSIE Va por buen camino
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.
Responder Con Cita
  #3  
Antiguo 30-07-2012
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 18.269
Poder: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
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":

Código Delphi [-]
  _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.
__________________
Germán Estévez => Web/Blog
Guía de estilo, Guía alternativa
Utiliza TAG's en tus mensajes.
Contactar con el Clubdelphi

P.D: Más tiempo dedicado a la pregunta=Mejores respuestas.
Responder Con Cita
  #4  
Antiguo 30-07-2012
elarys elarys is offline
Miembro
 
Registrado: abr 2007
Posts: 94
Poder: 18
elarys Va por buen camino
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.

Código Delphi [-]
//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;

Última edición por elarys fecha: 30-07-2012 a las 21:08:12.
Responder Con Cita
  #5  
Antiguo 30-07-2012
elarys elarys is offline
Miembro
 
Registrado: abr 2007
Posts: 94
Poder: 18
elarys Va por buen camino
Código Delphi [-]
{
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;
Responder Con Cita
  #6  
Antiguo 31-07-2012
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 18.269
Poder: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
5 lineas, son 5 líneas...
__________________
Germán Estévez => Web/Blog
Guía de estilo, Guía alternativa
Utiliza TAG's en tus mensajes.
Contactar con el Clubdelphi

P.D: Más tiempo dedicado a la pregunta=Mejores respuestas.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Publicar parte de un objeto de clase jlrbotella OOP 7 19-07-2017 09:18:54
Crear Objeto por su nombre de clase jlrbotella OOP 2 08-01-2008 23:44:37
Acceso a las propiedades de un objeto desde el editor de propiedades Hugo OOP 0 24-11-2006 12:58:22
Clase, objeto, tipo? [Gunman] OOP 3 04-01-2006 16:11:32
Metodo que devuelva la lista de propiedades de la clase. nemo OOP 1 16-07-2003 16:10:02


La franja horaria es GMT +2. Ahora son las 03:49:26.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi