Iterate Variable Variables

Is there a way to iterate member variables of an object in D2010 without knowing that they are well in advance?

+5
source share
1 answer

Yes, if you are using Delphi 2010 or later. You can use the extended RTTI to get information about object fields, methods, and properties. Simple version:

procedure GetInfo(obj: TObject);
var
  context: TRttiContext;
  rType: TRttiType;
  field: TRttiField;
  method: TRttiMethod;
  prop: TRttiProperty;
begin
  context := TRttiContext.Create;
  rType := context.GetType(obj.ClassType);
  for field in rType.GetFields do
    ;//do something here
  for method in rType.GetMethods do
    ;//do something here
  for prop in rType.GetProperties do
    ;//do something here
end;

Necessary objects can be found in the block RTTI.

In earlier versions of Delphi, there is an even more limited RTTI that can get you some information about some properties and methods, but it cannot do so much.

+2
source

All Articles