mheacock
asked on
Finding Published Properties At Run-time
I am in the process of designing a generic visual designer that
will be used in a few projects we are starting...in some of our
initial conversations I came up with the idea (since Java can do
it) that it would be really valuable if Delphi could find all
the properties/fields (public/published) of any component.
The idea is this...use right-clicks on some object on a form...
Delphi examines the object and builds a property list of all the
accessible fields of that object so that the user can set
and view them.
JavaBeans allow this kind of thing...and considering the elegance of
Delphi's RTTI, I figured it was probably possible in Delphi too.
Besides just getting the fields/properties of a component...is
it possible to also get the data type of the property?? I would
suspect that if the former is true, then the latter is true too.
And finally...where in the help files should I go looking to start
getting this information??
will be used in a few projects we are starting...in some of our
initial conversations I came up with the idea (since Java can do
it) that it would be really valuable if Delphi could find all
the properties/fields (public/published) of any component.
The idea is this...use right-clicks on some object on a form...
Delphi examines the object and builds a property list of all the
accessible fields of that object so that the user can set
and view them.
JavaBeans allow this kind of thing...and considering the elegance of
Delphi's RTTI, I figured it was probably possible in Delphi too.
Besides just getting the fields/properties of a component...is
it possible to also get the data type of the property?? I would
suspect that if the former is true, then the latter is true too.
And finally...where in the help files should I go looking to start
getting this information??
I assume you'd prefer comments first on this one too.
AFAIK you won't find any RTTI documentation in the helpfiles. You need 3rd-party books.
But the code in source\vcl\TypInfo.pas is quite readable, a bit of trying and failing ought to get you going.
Here's some code which'll add property Name, type and value(s) for most (I've skipped a few typekinds) published properties of any object. Just reconstruct the form and step through it. Delphi only generates RTTI for published properties, not public.
Code's tested with D3, it might be somewhat different using D2 but I doubt it.
--
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
TypInfo;
{$R *.DFM}
function TypeName (PropInfo : PPropInfo) : STRING;
begin
Result := PropInfo^.PropType^^.Name;
end;
function EnumeratedName (PropInfo : PPropInfo; OrdValue : INTEGER) : STRING;
begin
Result := GetEnumName(PropInfo^.Prop Type^, OrdValue);
end;
function SetMemberName (PropInfo : PPropInfo; Index : INTEGER) : STRING;
begin
Result := GetEnumName(GetTypeData(Pr opInfo^.Pr opType^)^. CompType^, Index);
end;
procedure GetPropInfo (AObject : TObject; PropInfo : PPropInfo; AStrings : TStrings);
var
s, v : STRING;
ndx,
OrdValue : INTEGER;
FloatValue : EXTENDED;
begin
S := 'Name = '+PropInfo^.Name+
', Type = ' + TypeName(PropInfo) +
', Value = ';
case PropInfo^.PropType^^.Kind of
tkUnknown : v := 'Unknown';
tkInteger : v := IntToStr(GetOrdProp(AObjec t, PropInfo));
tkChar : v := '#'+IntToStr(GetOrdProp(AO bject, PropInfo));
tkFloat : v := FloatToStrF(GetFloatProp(A Object, PropInfo), ffFixed, 18, 2);
tkLString,
tkString : v := GetStrProp(AObject, PropInfo);
tkSet :
begin
OrdValue := GetOrdProp(AObject, PropInfo);
for ndx := 0 to 31 do begin
if ( (1 shl ndx) and OrdValue) <> 0 then begin
if v = '' then
v := SetMemberName(PropInfo, ndx)
else
v := v + ', ' + SetMemberName(PropInfo, ndx);
end;
end;
v := '['+v+']';
end;
tkEnumeration : v := EnumeratedName (PropInfo, GetOrdProp(AObject, PropInfo));
tkClass : v := '$'+IntToHex(GetOrdProp(AO bject, PropInfo), 8);
end;
AStrings.Add(s+v);
end;
procedure GetObjectRTTI (AObject : TObject; AStrings : TStrings);
var
PropList : PPropList;
ndx : INTEGER;
PropCount : INTEGER;
begin
PropCount := GetTypeData(AObject.ClassI nfo)^.Prop Count;
GetMem(PropList, PropCount * SizeOf(POINTER));
GetPropList(AObject.ClassI nfo, tkAny, PropList);
for ndx := 0 to PropCount-1 do
GetPropInfo (AObject, PropList^[ndx], AStrings);
FreeMem(PropList);
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
GetObjectRTTI (StringGrid1, Listbox1.Items);
end;
end.
---
Regards,
Erik.
AFAIK you won't find any RTTI documentation in the helpfiles. You need 3rd-party books.
But the code in source\vcl\TypInfo.pas is quite readable, a bit of trying and failing ought to get you going.
Here's some code which'll add property Name, type and value(s) for most (I've skipped a few typekinds) published properties of any object. Just reconstruct the form and step through it. Delphi only generates RTTI for published properties, not public.
Code's tested with D3, it might be somewhat different using D2 but I doubt it.
--
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
TypInfo;
{$R *.DFM}
function TypeName (PropInfo : PPropInfo) : STRING;
begin
Result := PropInfo^.PropType^^.Name;
end;
function EnumeratedName (PropInfo : PPropInfo; OrdValue : INTEGER) : STRING;
begin
Result := GetEnumName(PropInfo^.Prop
end;
function SetMemberName (PropInfo : PPropInfo; Index : INTEGER) : STRING;
begin
Result := GetEnumName(GetTypeData(Pr
end;
procedure GetPropInfo (AObject : TObject; PropInfo : PPropInfo; AStrings : TStrings);
var
s, v : STRING;
ndx,
OrdValue : INTEGER;
FloatValue : EXTENDED;
begin
S := 'Name = '+PropInfo^.Name+
', Type = ' + TypeName(PropInfo) +
', Value = ';
case PropInfo^.PropType^^.Kind of
tkUnknown : v := 'Unknown';
tkInteger : v := IntToStr(GetOrdProp(AObjec
tkChar : v := '#'+IntToStr(GetOrdProp(AO
tkFloat : v := FloatToStrF(GetFloatProp(A
tkLString,
tkString : v := GetStrProp(AObject, PropInfo);
tkSet :
begin
OrdValue := GetOrdProp(AObject, PropInfo);
for ndx := 0 to 31 do begin
if ( (1 shl ndx) and OrdValue) <> 0 then begin
if v = '' then
v := SetMemberName(PropInfo, ndx)
else
v := v + ', ' + SetMemberName(PropInfo, ndx);
end;
end;
v := '['+v+']';
end;
tkEnumeration : v := EnumeratedName (PropInfo, GetOrdProp(AObject, PropInfo));
tkClass : v := '$'+IntToHex(GetOrdProp(AO
end;
AStrings.Add(s+v);
end;
procedure GetObjectRTTI (AObject : TObject; AStrings : TStrings);
var
PropList : PPropList;
ndx : INTEGER;
PropCount : INTEGER;
begin
PropCount := GetTypeData(AObject.ClassI
GetMem(PropList, PropCount * SizeOf(POINTER));
GetPropList(AObject.ClassI
for ndx := 0 to PropCount-1 do
GetPropInfo (AObject, PropList^[ndx], AStrings);
FreeMem(PropList);
end;
procedure TForm1.Button1Click(Sender
begin
GetObjectRTTI (StringGrid1, Listbox1.Items);
end;
end.
---
Regards,
Erik.
Humm... ygolan seem to have answered while I was writing...
Erik.
Erik.
ASKER
What an excellent answer...
Everyone else I asked (mailing lists, newsgroups) kept saying
"Check out Secrets of Delphi 2".
I'm not usually too keen on buying books...
Anyhow, all I usually need is a good pointer in the right
direction and away I go...typinfo.pas and your code has
helped me...I'm already 30% of the way through my visual
designer...thanks!!!
Everyone else I asked (mailing lists, newsgroups) kept saying
"Check out Secrets of Delphi 2".
I'm not usually too keen on buying books...
Anyhow, all I usually need is a good pointer in the right
direction and away I go...typinfo.pas and your code has
helped me...I'm already 30% of the way through my visual
designer...thanks!!!
ASKER
"Mark a comment as an answer" doesn't work...please post a
short note, sperling, that says "Grade me!".
short note, sperling, that says "Grade me!".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the code.
The information you need is accessed through the TypInfo unit, you can find an example that does it all in the TWriter.WriteProperties and TWriter.WriteProperty methods of TWriter, defined in Classes.pas.
I've implemented a fully working Delphi-like Object Inspector, so you can be assured that this can be implemented.
If you need source code to help you, I am afraid you will need to further raise the points for this question.