Solved

Read published properties at run-time.

Posted on 2000-03-01
5
166 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

11 Experts available now in Live!

Get 1:1 Help Now