Dr. Serg
Dr. Serg

Reputation: 139

RTTI: How get values of dynamic array declared as class property

Please, help me.

I looked for exists questions and no found how i can get all published property of items (declared as Class) in dynamic array in delphi class (i use Delphi 7 IDE (i can't use other version))

I have this code:

  TObjectList = array of TObject;
  TSubClass = class(TObject)
  private
    FFirstName: string;
    FLastName: string;
    FDOB: TDateTime;
    FArray : TObjectList;
  published
    property FirstName: string read FFirstName write FFirstName;
    property LastName: string read FLastName write FLastName;
    property DOB: TDateTime read FDOB write FDOB;
    property MyArray : TObjectList read FArray write FArray ;
  end;

  TListSubClass = array of TSubClass;

  TPersonList = class(TObject)
  private
    FSubClasses: TListSubClass;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property SubClasses: TListSubClass read FSubClasses write FSubClasses;
  end;

I have link to Elem of TPersonList class (MyVariable : TPersonList).

How i can using RTTI get all published property's data of my FSubClasses and FArray array items?

How i can set new data to FSubClasses using RTTI?

Thank you, Sergey.

Upvotes: 2

Views: 3635

Answers (2)

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43023

What you call "dynamic array" is not what is called a "dynamic array" in the Delphi world. A "dynamic array" is defined as MyVar: array of integer for instance. In your classes, you've only TList descendants. These TList descendants are some kind of dynamic storage, but it's called a TList (or TObjectList), instead of "dynamic array".

So just use the TypInfo unit.

  • GetPropList will get you the list of all properties.
  • Then call GetObjectProp for every PPropInfo item which maps a class, and retrieve the instance of every property.
  • Call GetStrProp to retrieve the content of a string published property;
  • Call GetOrdProp to retrieve the content of an integer published property.
  • Call GetFloatProp to get a floating point value, like a TDateTime.

In case of a class published property, after having called GetObjectProp, check the returned instance type, and enumerate its content, according to its class (TObjectList or TListSubClass).

It's such a method we use in our Open Source ORM (we've dedicated some object-oriented classes for properties access, so we don't need the typinfo unit). See http://synopse.info/fossil/finfo?name=SQLite3/SQLite3Commons.pas

Upvotes: 2

Jens Mühlenhoff
Jens Mühlenhoff

Reputation: 14873

Have a look at GetDynArrayProp and GetPropList of unit TypInfo.

GetDynArrayProp returns a pointer to the underlying array, you can then cast it to the correct array type.

GetPropList returns a pointer to an array of property information to all properties of the class you pass in.

The TPropInfo record that you get back from GetPropList has information on the address of the getter and setter methods associated with a property, you can use them to call the getter or setter respectively.

In general you should have a deeper look on the TypInfo unit in your Delphi help or at the online documentation:

http://docwiki.embarcadero.com/VCL/en/TypInfo

Upvotes: 1

Related Questions