Solved

Read published properties at run-time.

Posted on 2000-03-01
5
170 Views
Last Modified: 2010-04-04
I can't remember how to read the names of all published properties at run-time.

Peter
0
Comment
Question by:PeterLarsen
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2573295
hi peter,
i hope this is what your after as longish post :o)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    ListBox1: TListBox;
    procedure SpeedButton1Click(Sender: TObject);
  private
   FFred: integer;
published
    property Fred: integer read FFred write FFred;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

{$R *.DFM}
uses
  TypInfo;
procedure ListProperties(AInstance: TPersistent; AList: TStrings);
var
  i:        integer;
  pInfo:    PTypeInfo;
  pType:    PTypeData;
  propList: PPropList;
  propCnt:  integer;
  tmpStr:   string;
begin
  pInfo := AInstance.ClassInfo;

  if (pInfo = nil) or (pInfo^.Kind <> tkClass) then
    raise Exception.Create('Invalid type information');

  pType := GetTypeData(pInfo); // Pointer to TTypeData.

  AList.Add('Class name: ' + pInfo^.Name);

  // If any properties, add them to the list.
  //
  propCnt := pType^.PropCount;

  if propCnt > 0 then begin
    AList.Add (EmptyStr);
    tmpStr := IntToStr(propCnt) + ' Propert';

    if propCnt > 1 then
      tmpStr := tmpStr + 'ies'
    else
      tmpStr := tmpStr + 'y';

    AList.Add(tmpStr);
    FillChar(tmpStr[1], Length(tmpStr), '-');
    AList.Add(tmpStr);

    // Get memory for the property list.
    //
    GetMem(propList, sizeOf(PPropInfo) * propCnt);
    try
      // Fill in the property list.
      //
      GetPropInfos(pInfo, propList);

      // Fill in info for each property.
      //
      for i := 0 to propCnt - 1 do
        AList.Add(propList[i].Name + ': ' + propList[i].PropType^.Name);
    finally
      FreeMem(propList, sizeOf(PPropInfo) * propCnt);
    end;
  end;
end;

function GetPropertyList(AControl: TPersistent; AProperty: string):
PPropInfo;
var
  i:        integer;
  props:    PPropList;
  typeData: PTypeData;
begin
  Result := nil;

  if (AControl = nil) or (AControl.ClassInfo = nil) then
    Exit;

  typeData := GetTypeData(AControl.ClassInfo);

  if (typeData = nil) or (typeData^.PropCount = 0) then
    Exit;

  GetMem(props, typeData^.PropCount * SizeOf(Pointer));
  try
    GetPropInfos(AControl.ClassInfo, props);

    for i := 0 to typeData^.PropCount - 1 do begin
      with Props^[i]^ do
        if (Name = AProperty) then
          result := Props^[i];
    end;
  finally
    FreeMem(props);
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  c: integer;
begin
  ListProperties(self, ListBox1.Items);

  for c := 0 to ComponentCount - 1 do begin
    ListBox1.Items.Add(EmptyStr);

    ListProperties(Components[c], ListBox1.Items);
  end;


end;

end.
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 2573356
I think there is a easier way to do this - something  like GetName[x].

Regards
Peter
0
 
LVL 17

Expert Comment

by:inthe
ID: 2573586
possibly but not that i know of..the above came from marco cantu's Delphi Developer's Handbook.
i understand delphi5's typinfo unit has better functions maybe take a look at it (i dont have d5)
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 2575612
That Delphi instruction i'm talking about - with that it is only possible to read published properties.

Does your sample read any kind of properties ??

Regards
Peter
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 2575909
I have found a similar sample as yours - maybe this is the only way to do it :

http://community.borland.com/delphi/article/0,1410,16457,00.html.

procedure TForm1.FormCreate(Sender: TObject);
     var
       PropList: PPropList;
       i: integer;
     begin
       PropList := AllocMem(SizeOf(PropList^));
       i := 0;
       try
         GetPropList(TForm.ClassInfo, tkProperties + [tkMethod], PropList);
         while (PropList^[i] <> Nil) and (i < High(PropList^)) do
         begin
           ListBox1.Items.Add(PropList^[i].Name + ': '  +
             PropList^[i].PropType^.Name);
           Inc(i);
         end;
       finally
         FreeMem(PropList);
       end;
     end;

Thanks for your time
Peter
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reconfigure Delphi Install? 2 65
Performance of SQL statement 37 137
DBCtrlGrid, Delphi, Scroll 8 57
DBCtrlGrid, Delphi, Scroll 7 32
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

761 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