Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to get the published properties of an persistent object Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
How to get the published properties of an persistent object 21-Mar-07
Delphi All Versions
User Rating
No Votes
# Votes
Wittfoth, Boris Benjamin
Reference URL:
			The TypeInfo unit of Delphi declares several types and functions that gives you 
easy access to the puplished properties of an object and other informations. 
You can obtain a list of the published properties of a class and get the name an 
type of each property. 

The TypeInfo funtion returns a pointer to a type information record. The TypInfo 
unit declares a real type, that is, a pointer to a TTypeInfo record : 
2     PTypeInfo = ^TTypeInfo; 
3     TTypeInfo = record 
4       Kind: TTypeKind; 
5       Name: ShortString; 
6     end; 

The TTypeKind datatype describes the Datatype , returned by the GetTypeData 
9     TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat, 
10      tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString, 
11      tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray); 
12    TTypeKinds = set of TTypeKind; 

Well ... for our first step to access the objects published properties we need to 
use the PPropInfo-pointer. 
16    PPropInfo = ^TPropInfo; 
17    TPropInfo = packed record 
18      PropType: PPTypeInfo; 
19      GetProc: Pointer; 
20      SetProc: Pointer; 
21      StoredProc: Pointer; 
22      Index: Integer; 
23      default: Longint; 
24      NameIndex: SmallInt; 
25      Name: ShortString; 
26    end; 

To clarify it, please take a look at this example : 
28  function GetFontSize(Obj:TPersistent):Integer; 
29  { 
30  in this Procedure we want to get the pPropInfo-pointer - pointing 
31  on the Font-Property from an arbitrary TPersistent-Class. 
32  The return-value in this instance will be the font-size ( if the font 
33  property exists , if not -> the return value will be -1 ) 
34  } 
35  var PropInfo:PPropInfo; 
36  begin 
37    RESULT:=-1; 
38    // Get the PPropInfo-Pointer for Font of the TPersistent obj 
39    PropInfo:=GetPropInfo(Obj,'Font'); 
40    // At first we will find out if the property FONT exists 
41    if PropInfo=nil then 
42      EXIT; // The Property doesn't exists 
43    { 
44      TFont is not an ordinal-Type - therefore will have to control if 
45      Typekind of the TypeInfo-Class is set to tkClass 
46    } 
47    if PropInfo.PropType^.Kind <>tkClass then 
48      EXIT; // property isn't a tkClass type 
49    { 
50      now, we now that the TypeKind of die PropInfo-pointer is a class . 
51      last but not least we will use the GetObjectProp, the return-value 
52      of this function is a TObject. Subsequently, we will use this object as 
53      a TFont to get the Size value. 
54    } 
55    RESULT:=( (GetObjectProp(Obj,PropInfo)) as TFont).Size; 
56  end; 

But to get the complete list of all properties of a TPersistent-Class we will need 
the pPropList-Type . This type is a simple pointer-array and the magic key to all 
Property-Informations and their structures. 

Take a look at this : 
58  procedure TForm1.Button1Click(Sender: TObject); 
60  const tkOrdinal = [tkEnumeration,tkInteger,tkChar,tkSet,tkWChar]; //Filter 
62  begin 
63  { 
64    in this method of the mainform-class we are seeking for all ordinal-type 
65    properties of the edit1-component. The from the GetPropertyList method 
66    returned list of all properties will be written into the Listbox1. You can 
67    replace the obj parameter with an arbitrary TObject ( but usually TPersistent 
68    objects ). 
69    For another filter please take a look at the TTypeKinds-set. 
70  } 
71    GetPropertyList(Edit1,ListBox1.Items,tkOrdinal); 
72  end; 
74  procedure GetPropertyList(Obj:TObject ;List: TStrings;Filter: TTypeKinds); 
75  var PropList:pPropList; 
76      count,i : Integer; 
77  begin 
78    List.Clear; 
79    // Here we'll get the count of the given properties, ... 
80    Count := GetPropList(Obj.ClassInfo, Filter, nil); 
81    // ...and create room for the PropList,... 
82    GetMem(PropList,Count * SizeOf(PPropInfo)); 
83    // ...get the Proplist-Data,... 
84    GetPropList(Obj.ClassInfo, Filter, PropList); 
85    // ...and write the property-names into the StringList 
86    for i:=0 to Count -1 do 
87      List.Add(Proplist[i].Name); 
88  end; 

That's it ---- actually easy , or? 

best regards 

Boris Benjamin Wittfoth 

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC