Delphi – Object list

How to get the property list of Object that is not Component (at runtime). Just like Grid Cell, it has its own properties (Font, Align, etc.).

Grid such as AdvStringGrid or AliGrid, or Bergs NxGrid.

What you require is access The RTTI (Runtime Type Information) of the object.

If you are using Delphi 2009 or earlier, RTTI only exposes published properties and published methods (i.e. event handlers) ). Check the GetPropInfos() or GetPropList() functions in the System.TypInfo unit. They provide you with a series of pointers to TPropInfo record pointer, each record corresponds to an attribute. TPropInfo has a Name member (among other things).

uses
TypInfo;

var
PropList: PPropList;
PropCount, I: Integer;
begin
PropCount := GetPropList(SomeObject, PropList);
try< br /> for I := 0 to PropCount-1 do
begin
// use PropList[I]^ as needed...
ShowMessage(PropList[I].Name);< br /> end;
finally
FreeMem(PropList);
end;
end;

Please note that this type of RTTI only applies to deriving from Tpersistent , Or apply the (M+) compiler directive (TPersistent does this).

If you are using Delphi 2010 Or later, regardless of its visibility, the extended RTTI will expose all attributes, methods and data members. Check the TRttiContext record in the TRttiContext unit and the TRttiType and TRttiProperty classes. For more details, please refer to Embarcadero’s documentation: Working with RTTI.

uses
System.Rtti;

var
Ctx: TRttiContext;
Typ: TRttiType;
Prop: TRttiProperty;
begin
Typ := Ctx .GetType(SomeObject.ClassType);
for Prop in Typ.GetProperties do
begin
// use Prop as needed...
ShowMessage(Prop.Name);
end;
for Prop in Typ.GetIndexedProperties do
begin
// use Prop as needed...
ShowMessage(Prop.Name);
end;
end;

How to get the property list of Object that is not Component (at runtime). Just like Grid Cell, it has its own properties (Font, Align, etc.).

Grid such as AdvStringGrid or AliGrid, or Bergs NxGrid.

What you require is to access the object RTTI (Runtime Type Information).

If you are using Delphi 2009 or earlier, RTTI only exposes published properties and published methods (i.e. event handlers). Check the GetPropInfos() or GetPropList( ) function. They provide you with a series of pointers to TPropInfo records, and each record corresponds to an attribute. TPropInfo has a Name member (among other things).

< p>

uses
TypInfo;

var
PropList: PPropList;
PropCount, I: Integer;
begin
PropCount := GetPropList(SomeObject, PropList);
try
for I := 0 to PropCount-1 do
begin
// use PropList[I]^ as needed ...
ShowMessage(PropList[I].Name);
end;
finally
FreeMem(PropList);
end;
end;< /pre>

Please note that this type of RTTI only applies to classes derived from TPersistent, or that the (M+) compiler directive is applied (TPersistent does this).

If you are using Delphi 2010 or later, regardless of its visibility, extended RTTI will expose all properties, methods and data members. See TRttiContext in the TRttiContext unit code>Record and TRttiType and TRttiProperty classes. For more details, please refer to Embarcadero's documentation: Working with RTTI.

uses
System.Rtti;

var
Ctx: TRttiContext;
Typ: TRttiType;
Prop: TRttiProperty;
begin
Typ := Ctx.GetType(SomeObject.ClassType);
for Prop in Typ.GetProperties do
begin
// use Prop as needed...
ShowMessage(Prop.Name);
end;
for Prop in Typ.GetIndexedProperties do
begin
// use Prop as needed...
ShowMessage(Prop.Name);
end;
end;

Leave a Comment

Your email address will not be published.