Solved

Finding Published Properties At Run-time

Posted on 1997-07-04
7
339 Views
Last Modified: 2010-04-06
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??
0
Comment
Question by:mheacock
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:ygolan
ID: 1338216
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
 
LVL 3

Expert Comment

by:sperling
ID: 1338217
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
 
LVL 3

Expert Comment

by:sperling
ID: 1338218
Humm... ygolan seem to have answered while I was writing...

Erik.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

by:mheacock
ID: 1338219
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
 
LVL 3

Author Comment

by:mheacock
ID: 1338220
"Mark a comment as an answer" doesn't work...please post a
short note, sperling, that says "Grade me!".


0
 
LVL 3

Accepted Solution

by:
sperling earned 200 total points
ID: 1338221
Okie, grade me!  ;)


Regards,

Erik.
0
 
LVL 3

Author Comment

by:mheacock
ID: 1338222
Thanks for the code.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now