Suite101

RTTI Part 2. Getting object property listing.


© Lyapin Ilya

It's possible to get the names of an object's properties through the RTTI. Let's look at typinfo.pas unit and see GetPropList function.

function GetPropList(TypeInfo: PTypeInfo; TypeKinds: TTypeKinds;PropList: PPropList): Integer;

The first argument is a pointer to an object's RTTI information. You can use the ClassInfo property of an object to obtain this pointer.

The second parameter is a set of the property types. TTypeKind enumerated type is defined as foolowing

TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,
tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString,
tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray);
TTypeKinds = set of TTypeKind;

Note that tkInt64 and tkDynArray constants are included only in Delphi 4. There are no corresponding types (Int64 and Dynamic array) in Delphi 3. TTypeKinds is a set of TTypeKind values.
Typinfo.pas also declares several useful constants:

const
tkAny = [Low(TTypeKind)..High(TTypeKind)];
tkMethods = [tkMethod];
tkProperties = tkAny - tkMethods - [tkUnknown];

The third argument is a pointer to an array of TPropInfo records. Each record holds information about particular property. TPropInfo record includes fields such as Name or PropType.
GetPropList returns the number of the properties returned in PropList.

Working with components by number.

Each object of the TComponent derived class (for example Form) provides indexed access to all components owned by it through Components and ComponentCount properties.

property ComponentCount: Integer;
property Components[Index: Integer]: TComponent;

FindComponent function returns a reference to an instance of an object based on string - name of the component.

function FindComponent(const AName: string): TComponent;

Code example

The use of GetPropList is demonstrated in the PROPLIST project. The main form is shown in Figure 1. This form includes two comboboxes whose contents are populated at run-time with the form's onCreate event handler. ComboBox1 is filled with TTypeKind values. This part of the code was discussed in the previous article. ComboBox2 is filled with the names of the components appearing on the form.

When user changes either ComboBox text, onChange event occurs. The program stores a kind of properties to retrieve in the tk variable. This part of code uses GetEnumValue function. Refer the previous article for details. If user typed wrong value or leave ComboBox1 empty, the program will retrieve the names of properties of any kind. (See tkAny constant declaration above).

The ListBox1 is populated with the property names of the object selected in the ComboBox2. This code is generic because the PPropInfo is extracted using the ClassInfo property of a component, a pointer to which is returned by

Go To Page: 1 2


The copyright of the article RTTI Part 2. Getting object property listing. in Delphi Programming is owned by Lyapin Ilya. Permission to republish RTTI Part 2. Getting object property listing. in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo


Here's the follow-up discussion on this article: View all related messages

1.   Oct 12, 2005 2:19 AM
Hi,
I read the part 2 of your discussion which is about getting object property. Your discussion showed how to get the property of the component by separating the what kind of property it is (i.e. tk ...

-- posted by PatS14





For a complete listing of article comments, questions, and other discussions related to Lyapin Ilya's Delphi Programming topic, please visit the Discussions page.