RTTI Part 5. Summary.
GetPropList(obj.ClassInfo, tkAll, ppList);
GetPropInfos retrieves data for all objects' properties,
GetPropList allows to specify a kind (several kinds) of the
properties. So if you need only string properties use following code:
GetPropList( Obj.ClassInfo ,[ tkString, tkLString, tkWString], nil);
Declarations of these routines are the same in all Delphi versions.
GetXXX and SetXXX routines.
Through the RTTI you can access property if you know its name. The RTTI provides a series of functions with names
like SetOrdProp, GetOrdProp, SetStrProp,GetStrProp and so forth.
function Get<PropType>Prop(Instance: TObject; PropInfo: PPropInfo): <PropType>;
procedure Set<PropType>Prop(Instance: TObject; PropInfo: PPropInfo;
Value: <PropType>);
The first parameter is the object instance, the second is PPropInfo reference to property. The GetXXX functions return
value of the property; the SetXXX functions receive a new value as the third parameter.
Each type has its own GetXXX/SetXXX pair. Here is a full listing of these routines:
function GetOrdProp(Instance: TObject; PropInfo: PPropInfo): Longint;
procedure SetOrdProp(Instance: TObject; PropInfo: PPropInfo;
Value: Longint);
function GetStrProp(Instance: TObject; PropInfo: PPropInfo): string;
procedure SetStrProp(Instance: TObject; PropInfo: PPropInfo;
const Value: string);
function GetFloatProp(Instance: TObject; PropInfo: PPropInfo): Extended;
procedure SetFloatProp(Instance: TObject; PropInfo: PPropInfo;
Value: Extended);
function GetVariantProp(Instance: TObject; PropInfo: PPropInfo): Variant;
procedure SetVariantProp(Instance: TObject; PropInfo: PPropInfo;
const Value: Variant);
function GetMethodProp(Instance: TObject; PropInfo: PPropInfo): TMethod;
procedure SetMethodProp(Instance: TObject; PropInfo: PPropInfo;
const Value: TMethod);
The GetInt64Prop and SetInt64Prop routines are added in Delphi 4 in order to access
properties of type Int64.
function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64;
procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo;
const Value: Int64);
The following routines are added in Delphi 5
function GetEnumProp(Instance: TObject; PropInfo: PPropInfo): string; procedure
SetEnumProp(Instance: TObject; PropInfo: PPropInfo;
const Value: string);
function GetSetProp(Instance: TObject; PropInfo: PPropInfo;
Brackets: Boolean = False): string;
procedure SetSetProp(Instance: TObject; PropInfo: PPropInfo;
const Value: string);
New Delphi 5 features.
There are several so-called 'easy access' routines. They overload standard routines listed above. Each
GetXXX/SetXXX routine has an 'easy access' version. The only difference - type of second parameter is string, and the
name of property should be specified instead of PPropinfo reference. These routines are implemented like following
function GetEnumProp(Instance: TObject; const PropName: string): string;
begin
Result := GetEnumProp(Instance, GetPropInfo(Instance, PropName));
end;
procedure SetEnumProp(Instance: TObject; const PropName: string;
const Value: string);
begin
SetEnumProp(Instance, GetPropInfo(Instance, PropName), Value);
end;
The call to GetPropInfo is hidden in the 'easy access' routine body.
I prefer to use old style routines in order to make the source code that can be compiled and do the same under all
Delphi versions.
Summary.
I hope that this article will simplify usage of undocumented Delphi features. I spent several hours analyzing the
typinfo.pas unit sources to retrieve this information, I'm not sure that all is correct so if you find a bug please let me
know.
The copyright of the article
RTTI Part 5. Summary. in
Delphi Programming is owned by Lyapin Ilya. Permission to republish
RTTI Part 5. Summary. in print or online must be granted by the author in writing.
Go To Page:
1
2
Articles in this Topic
Discussions in this Topic