Get the name of an attribute of an object

2

I have the following class

TLog = class
private
  FDescricao: string;
  FCodigo: Integer;
public
  property Codigo: Integer read FCodigo write FCodigo;
  property Descricao: string read FDescricao write FDescricao;
end;

I want to create a method that gets the name of the attribute (from the instantiated object) in String. Example:

ConverEmString(Log.Codigo) = "Codigo"
ConverEmString(Log.Descricao) = "Descrição"

Is it possible to do this? If so, how?

    
asked by anonymous 17.11.2014 / 12:15

1 answer

2

You can use the delphi RTTI. If it is any version later than 2010 you can use the new RTTI. But I'll set the example with the old one that works on everyone since version 5

var
  oPropList: TPropList;
begin
  GetPropList(seuObjeto.ClassInfo, tkProperties, @oPropList);

Using the GetPropList method, which returns the object's published properties list, in the third @PropList parameter (@ is used because the expected parameter is of type PPropList which is a pointer to TPropList) .

This list is an array of fixed size, so when going through the array, each interaction is necessary to check if the value of the array item is nil and esiver, give a break and end the loop:

  for I := Low(oPropList) to High(oPropList) do
  begin
    if not Assigned(oPropList[I]) then
      Break;

    ShowMessage(oPropList[I]^.Name);
  end;

Each Array item is a PropInfo, which has property information, such as type, name, Get and Set methods, default values, index, etc. Remark: The PropInfo Object has only the property information, not its value in any instance of the class.

  TPropInfo = packed record
    PropType: PPTypeInfo;
    GetProc: Pointer;
    SetProc: Pointer;
    StoredProc: Pointer;
    Index: Integer;
    Default: Longint;
    NameIndex: SmallInt;
    Name: ShortString;
  end;

Given these data and the methods quoted below, we can get all published data from the Object

  label.caption = oPropList[I]^.name;
  case oPropList[I]^.PropType^.Kind of
    tkString, tkWChar, tkLString, tkWString, tkChar: Label.Caption :=
        Label.Caption + ' : ' + GetStrProp(seuObjeto, oPropList[I]);
    tkInteger: Label.Caption :=
        Label.Caption + ' : ' + IntToStr(GetOrdProp(seuObjeto, oPropList[I]));
    tkClass: ;
  end;

Note that checking by type allows us to choose which method we will use to get the value of the property, with the methods specific to each type of property.

The expected types in PropType are:

tkUnknown
tkInteger
tkChar
tkEnumeration
tkFloat,
tkString
tkSet
tkClass
tkMethod
tkWChar
tkLString
tkWString,
tkVariant
tkArray
tkRecord
tkInterface
tkInt64
tkDynArray

The Get and Set methods of properties are based on their types, and can receive the property name or a PPropInfo per parameter, as in this example. The methods according to the types that can be used to get the values are:

GetOrdProp (Inteiros)
GetEnumProp (Tipos enumerados)
GetSetProp (_Sets)
GetObjectProp (Objetos)
GetStrProp (Strings)
GetFloatProp (Pontos Flutuantes)
GetVariantProp (Variant)
GetMethodProp (Métodos)
GetInt64Prop (Inteiros de 64 bits)
  

Note: Attributes to be obtained must have visibility Published

    
17.11.2014 / 15:06