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??
LVL 3
mheacockAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ygolanCommented:
You can find all the information displayed in the Delphi Object Inspector at runtime, for any *published* property.

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.
0
sperlingCommented:
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^.PropType^, OrdValue);
end;

function SetMemberName (PropInfo : PPropInfo; Index : INTEGER) : STRING;
begin
  Result := GetEnumName(GetTypeData(PropInfo^.PropType^)^.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(AObject, PropInfo));
    tkChar    : v := '#'+IntToStr(GetOrdProp(AObject, PropInfo));
    tkFloat   : v := FloatToStrF(GetFloatProp(AObject, 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(AObject, 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.ClassInfo)^.PropCount;
  GetMem(PropList, PropCount * SizeOf(POINTER));
  GetPropList(AObject.ClassInfo, 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.
0
sperlingCommented:
Humm... ygolan seem to have answered while I was writing...

Erik.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

mheacockAuthor Commented:
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!!!
0
mheacockAuthor Commented:
"Mark a comment as an answer" doesn't work...please post a
short note, sperling, that says "Grade me!".


0
sperlingCommented:
Okie, grade me!  ;)


Regards,

Erik.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mheacockAuthor Commented:
Thanks for the code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.