Link to home
Start Free TrialLog in
Avatar of mheacock
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??
Avatar of ygolan
ygolan

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.
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.
Humm... ygolan seem to have answered while I was writing...

Erik.
Avatar of mheacock

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


ASKER CERTIFIED SOLUTION
Avatar of sperling
sperling

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the code.