Get the name of an object attribute

Asked

Viewed 1,847 times

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 (of the instantiated object) in String. Example:

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

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

1 answer

3


You can use the Delphi RTTI. If it is a version later than 2010 you can use the new RTTI. But let me give you an 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 Properties list Published object, in the third @Proplist parameter (@ is used because the expected parameter is of type Pproplist which is a pointer to Tproplist).

This list is a fixed-size array, so when you go through the array, every interaction you need to check if the value of the array item is nil and if it’s esiver, break and close the loop:

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

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

Each item in the Array is a Propinfo, which has information from the Property, such as type, name, Get and Set methods, Default values, index, etc. Note: The Propinfo Object has only the information of the Property, not its value in some instance of the class.

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

In possession of these data and the methods cited below, we can obtain all the Published data of 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 type checking allows us to choose which method we will use to get the value of Property, with the methods specific to each type of Property.

The types expected 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 the properties are based on their types, and can receive by parameter the name of the Property or a Ppropinfo, as was done in this example. The methods according to the types that can be used to obtain 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 Published visibility

  • Nice, very well explained about Rtti. I’m using it in some parts of the project. But it didn’t answer what I asked! List I can, the question is how to get the name of the attribute (of the instantiated object) in String > Log.Description = "Description". I already know which attribute I want, but I need you to take his name in String. Got it?

  • I am using the Aurelius ORM. In it I need to pass the name of the string field to do Where queries. I want to avoid using direct string because, if I come to maintain and remove the class field/attribute using string, I will only discover possible errors when running the system. Already using "Log.Code" if you remove the Code field, it would already give error in the compilation, allowing knowing exactly the locations that are using this field. Got it?

  • Which version of Delphi? An idea came to mind with Classhelpers for basic types

  • Using Attributes is an option? if so, it is easy to solve

  • What do you mean Attributes? Can you give me an example?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.